Difference between revisions of "Set up Login System in Django"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Replaced content with "I'm currently updating this")
Line 1: Line 1:
I'm currently updating this
+
=Initial Setup=
 +
==Project URL's==
 +
If you have created a django project by running the 'startproject' option of django-admin, it will create a folder which contains a file called 'urls.py'. This is the file used to define the URL's accepted by your web app. For a login/logout system we can use the built in django methods, and will mainly need to add code into this file.
 +
 
 +
Open the 'urls.py', you will need to import the following at the top of the page:
 +
 
 +
<syntaxhighlight lang=python>
 +
from django.contrib.auth import views as auth_views
 +
</syntaxhighlight>
 +
 
 +
Next, you will see a section which defines 'urlpatterns', firstly check the following is defined:
 +
 
 +
<syntaxhighlight lang=python>
 +
path('admin/', admin.site.urls),
 +
</syntaxhighlight>
 +
 
 +
Next, we need to add some additional entries into this:
 +
 
 +
<syntaxhighlight lang=python>
 +
path('login/', auth_views.LoginView.as_view(), name='login'),
 +
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
 +
</syntaxhighlight>
 +
 
 +
==Settings==
 +
 
 +
Now in the project folder you should see a file called 'settings.py', we need to add this line:
 +
<syntaxhighlight lang=python>
 +
LOGIN_URL = 'login'
 +
</syntaxhighlight>
 +
 
 +
==User app==
 +
 
 +
Create a new app in your project called 'users', then in this new folder edit the 'apps.py' file, you should see something like this:
 +
 
 +
<syntaxhighlight lang=python>
 +
from django.apps import AppConfig
 +
 
 +
class UsersConfig(AppConfig):
 +
    name = 'myproject.users'
 +
</syntaxhighlight>
 +
 
 +
Copy the name of the class, the example above is called UsersConfig. Now goto the 'settings.py' file and find the installed apps definition:
 +
 
 +
<syntaxhighlight lang=python>
 +
INSTALLED_APPS = [
 +
    'django.contrib.admin',
 +
    'django.contrib.auth',
 +
    'django.contrib.contenttypes',
 +
    'django.contrib.sessions',
 +
    'django.contrib.messages',
 +
    'django.contrib.staticfiles',
 +
 
 +
]
 +
</syntaxhighlight>
 +
 
 +
Add a new entry at the start of this list for your app:
 +
 
 +
<syntaxhighlight lang=python>
 +
INSTALLED_APPS = [
 +
    'myproject.users.apps.UsersConfig',
 +
    'django.contrib.admin',
 +
    'django.contrib.auth',
 +
    'django.contrib.contenttypes',
 +
    'django.contrib.sessions',
 +
    'django.contrib.messages',
 +
    'django.contrib.staticfiles',
 +
]
 +
</syntaxhighlight>
 +
 
 +
==User Templates==

Revision as of 18:53, 1 May 2019

Initial Setup

Project URL's

If you have created a django project by running the 'startproject' option of django-admin, it will create a folder which contains a file called 'urls.py'. This is the file used to define the URL's accepted by your web app. For a login/logout system we can use the built in django methods, and will mainly need to add code into this file.

Open the 'urls.py', you will need to import the following at the top of the page:

from django.contrib.auth import views as auth_views

Next, you will see a section which defines 'urlpatterns', firstly check the following is defined:

path('admin/', admin.site.urls),

Next, we need to add some additional entries into this:

path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),

Settings

Now in the project folder you should see a file called 'settings.py', we need to add this line:

LOGIN_URL = 'login'

User app

Create a new app in your project called 'users', then in this new folder edit the 'apps.py' file, you should see something like this:

from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = 'myproject.users'

Copy the name of the class, the example above is called UsersConfig. Now goto the 'settings.py' file and find the installed apps definition:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

]

Add a new entry at the start of this list for your app:

INSTALLED_APPS = [
    'myproject.users.apps.UsersConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

User Templates