422
Chapter 19
Now Django will include the
users
app in the overall project.
Including the URLs from users
Next, we need to modify the root urls.py so it includes the URLs we’ll write
for the
users
app:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('', include('learning_logs.urls')),
]
We add a line to include the file urls.py from
users
. This line will match
any URL that starts with the word users, such as http://localhost:8000/users
/login/.
The Login Page
We’ll first implement a login page. We’ll use the default
login
view Django
provides, so the URL pattern for this app looks a little different. Make a new
urls.py file in the directory learning_log/users/, and add the following to it:
"""Defines URL patterns for users"""
from django.urls import path, include
u
app_name = 'users'
urlpatterns = [
# Include default auth urls.
v
path('', include('django.contrib.auth.urls')),
]
We import the
path
function, and then import the
include
function so
we can include some default authentication URLs that Django has defined.
These default URLs include named URL patterns, such as
'login'
and
'logout'
. We set the variable
app_name
to
'users'
so Django can distinguish
these URLs from URLs belonging to other apps u. Even default URLs pro-
vided by Django, when included in the
users
app’s urls.py file, will be acces-
sible through the
users
namespace.
The login page’s pattern matches the URL http://localhost:8000/users
/login/ v. When Django reads this URL, the word users tells Django to look in
users/urls.py, and login tells it to send requests to Django’s default
login
view.
Do'stlaringiz bilan baham: |