Learn Django

Create the Login & Logout URLs

As mentioned, instead of writing your own login and logout views, you can simply use Django's pre-built views. These views are easy to tap into. All you have to do is add the following lines of code to your URLconf. In addition to pointing to the views, we also pass in the template name that we created in the previous lesson.

Open /.../crmeasy/crmapp/urls.py in your IDE, locate the '# Login/Logout URLs' comment, and add this code beneath it.

1
2
3
4
5
6
7
# Login/Logout URLs
(r'^login/$',
    'django.contrib.auth.views.login', {'template_name': 'login.html'}
),
(r'^logout/$',
    'django.contrib.auth.views.logout', {'next_page': '/login/'}
),

Code Review

Line 2: The login URLconf specifics /login/ as the URL, points to the location of the auth system's login view, and then provides the customer login template we created in the previous chapter.

Line 3: The logout URLconf is much simpler. All it needs is the /logout/ URL, a pointer to the view, and the next_page setting. The next_page setting will determine where the user is directed to once the logout is complete.

Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "create login & logout URLs"

Track your progress with a free account