In the base app, which I call it "mywebsite" (the one that contains the settings of the django project), has the urls.py file. But I do not want to reference this file, I want to reference a urls.py in another app, which I call it "account".
For the base file, I would reference as {% url'login' %}, for example. How do I reference to the other one? Maybe {% account.url 'login' %}?
I tried {% account.url 'login' %} and {% account/url 'login' %}
CodePudding user response:
I see that you already answered your question, but with no further details or explanations. I'll provide the context needed.
You have a project package. This folders contains files that affect all the application (the website, not just an app). Inside we have the following files: settings, wsgi, asgi and urls.
In the urls.py (also called URLconf module) file you control the routing of your application, that is, the mapping between URL path expressions to Python functions (your views).
The Django frameworks knows where to look up for this file because it's stablished in the settings.py file:
ROOT_URLCONF = 'django_project.urls'
This is the process (from the docs)
- Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting...
- Django loads that Python module and looks for the variable urlpatterns. This should be a sequence of django.urls.path() and/or django.urls.re_path() instances.
- Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.
- Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view)...
But you also have apps, besides the project package. Each app can also have a URLconf module (app/urls.py).
To Django to know the routing to this apps, you have to include them in the global urls.py file (from the project package).
Finally, the answer:
How do you do it? With the include() function. As you mentioned in your own answer, the code is:
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("account.urls")),
]
You're including the accounts paths into the global URLconf module.
So when the user types www.your-domain.com/
he will access the accounts
app routes.
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include("account.urls")),
]
If instead you did like this, the user should type: www.your-domain.com/accounts/
.
CodePudding user response:
Never mind, I just added to the base urls.py file: path('account', include("account.urls"))