Error including app urls in project.py urls

Asked

Viewed 806 times

1

I’m at the beginning of a project with Django 2.2.1, creating the first apps and making the links between the pages. In the project’s urls.py, including the Accounts urls worked (with the include(accounts.urls), but when trying to do the same for the expenses app I’m not getting.

When running the runserver, error appears django.core.exceptions.ImproperlyConfigured: The included URLconf 'projeto.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

I have researched and tried several things I found, but nothing is working. If anyone can help I appreciate.

py. of the project

from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('expenses/', include('expenses.urls')),  # expenses app urls
    path('accounts/', include('accounts.urls')), #sign up
    path('accounts/', include('django.contrib.auth.urls')), #login and logout
    path('', TemplateView.as_view(template_name='home.html'), name='home'), #homepage
]

App files expenses:

py.

from django.urls import path
from . import views

urlpatterns = [
    path('create/', views.CreateExpenseView.as_view(template_name='create_expense.html'), name='create-expense'), #create new expense
]

py views.

from django.views.generic.edit import CreateView  
from .forms import ExpenseForm

#view for create a new expense
class CreateExpenseView(CreateView):
    form_class = ExpenseForm
    template_name = 'templates/create_expense.html'

py.models

from django.db import models

class Expense(models.Model):
    TYPE = (
        ('Type1', 'Type1'),
        ('Type2', 'Type2'),
        ('Type3', 'Type3'),
        ('Outros', 'Outros') #abre um campo para digitar
    )

    type = models.CharField(max_length=10, choices=TYPE, null=True, blank=True) 
    value = models.FloatField(null=True, blank=True)
    num_installments = models.IntegerField(null=True, blank=True) #parcelas
    day_installments = models.IntegerField(null=True, blank=True) #dia de pagamento de cada parcela

estrutura do projeto

  • 1

    path('create/', views.CreateExpenseView.as_view(template_name='create_expense.html'), name='create-expense') template_name = 'templates/create_expense.html' would not be better to declare the template only in the url or only in the view?

  • I tried to do it but it didn’t solve :/

1 answer

2


There are two urlpatterns accounts/:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('expenses/', include('expenses.urls')),  # expenses app urls
    path('accounts/', include('accounts.urls')), #sign up
    path('accounts/', include('django.contrib.auth.urls')), #login and logout
    path('', TemplateView.as_view(template_name='home.html'), name='home'), #homepage
]

Translated from documentation: https://docs.djangoproject.com/en/2.2/topics/http/urls/

Naming URL patterns

To perform URL reversal, you will need to use named URL patterns, as done in the examples above. The string used for the URL name can contain all the desired characters. You are not restricted to valid Python names.

When naming URL patterns, choose names that probably won’t collide with the choice of other app names. If you call your URL default comment and another application does the same thing, the URL that finds Reverse() will depend on the pattern that is last in your project’s url pattern list.

Prefixing your URL names, perhaps derived from the application name (such as myapp-comment instead of comment), decreases the chance of collision.

You can deliberately choose the same URL name as another application if you want to replace a view. For example, a common use case is to replace Loginview. Parts of Django and most third-party applications assume that this view has a URL pattern with the login name. If you have a custom login view and provide your URL with the login name, Reverse() you will find your custom view as long as it is in urlpatterns after Django.contrib.auth.urls is included (if included).

You can also use the same name for various URL patterns if they differ in their arguments. In addition to the URL name, Reverse() corresponds to the number of arguments and the names of the keyword arguments.

So how your standards are exactly equal, no parameters, may be the cause of the problem.

  • I believe this is no problem, I did it following a tutorial and was working normally, the problem is when adds the same expenses

  • I improved the answer with a quote from the documentation.

  • I took one of the Accounts/ and keeps giving the error, so I understood from the quote, in this case there is no problem because they refer to different apps

  • 2

    Let’s take a closer look at the error message: 1. The included Urlconf 'cedimfinanceiro.urls' does not appear to have any Patterns in it. ** Is your project name financial? So this file he claims is the.py urls of the project? Could you include your project structure in the post? 2. If you see Valid Patterns in the file then the Issue is probably caused by a circular import. ** If the cited file has valid url patterns, the issue may be circular import. The project structure can help clarify if there is such a possibility.

  • I edited and added the project structure

  • All of its init.py are empty? Just to remove a suspicion, could you do the following test? In the.py urls of expenses, change from . import views for from .views import createExpenseView, changing the call just below (removing views.)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.