今天來實現網站的登入和登出功能。html
首先咱們須要在urls.py中添加路徑,注意此處的路徑和在導航欄中設置的文字路徑保持一致:python
from django.urls import path from . import views app_name = 'main' #此處爲了urls的命名空間 urlpatterns = [ path('', views.homepage, name='homepage'), path('register/', views.register, name='register'), path('logout/', views.logout_request, name='logout_request'), path('login/', views.login_request, name='login_request'), ]
在view.py中添加登出和登入的函數,登出的函數很是簡單,只須要使用logout函數登出並給出登出成功的信息,而後導向主頁便可。登入函數須要使用新的表單AuthenticationForm,並判斷用戶是否存在,若是存在則登入並進入主頁,若是不存在則給出錯誤信息:django
from django.shortcuts import render, redirect from django.http import HttpResponse from .models import Tutorial from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from django.contrib.auth import login, logout, authenticate from django.contrib import messages # Create your views here. def homepage(request): return render(request=request, template_name='main/home.html', context={'tutorials':Tutorial.objects.all}) def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): user = form.save() username = form.cleaned_data.get('username') messages.success(request, f"New account created: {username}") login(request, user) return redirect('main:homepage') else: for msg in form.error_messages: messages.error(request, f"{msg}:{form.error_messages[msg]}") return render(request=request, template_name='main/register.html', context={'form':form}) form = UserCreationForm return render(request=request, template_name='main/register.html', context={'form':form}) def logout_request(request): logout(request) messages.info(request, f"Logged out successfully!") return redirect('main:homepage') def login_request(request): if request.method == 'POST': form = AuthenticationForm(request=request, data=request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) if user is not None: login(request, user) messages.info(request, f"You are now logged in as {username}") return redirect('main:homepage') else: messages.error(request, f"Invalid username or passowrd.") else: messages.error(request, f"Invalid username or passowrd.") form = AuthenticationForm return render(request=request, template_name='main/login.html', context={'form':form})
登入登出及信息顯示以下,第二幅圖顯示「You are now logged in as xiaohonghong」,這個就是toast的效果:瀏覽器
與以前的幾節不一樣,我在這裏修改了導航欄的顏色,並添加了container效果,修改方法以下,經過style設置背景顏色,將container加入到div的class中:app
<nav style="background-color: #008E72"> <div class="nav-wrapper container">
如今使用的註冊表單是materialize默認的,咱們但願註冊的時候能夠加入郵箱這個選項,這樣忘記密碼的時候就能夠經過郵箱來找回密碼,如何自定義註冊表單呢?方法以下,在main文件夾下新建forms.py:函數
from django.contrib.auth.forms import UserCreationForm from django import forms from django.contrib.auth.models import User class NewUserForm(UserCreationForm): email = forms.EmailField(required=True) class Meta: model = User fields = ("username", "email", "password1", "password2") def save(self, commit=True): user = super(NewUserForm, self).save(commit=False) user.email = self.cleaned_data.get('email') if commit: user.save() return user
而後將views.py中的UserCreationForm修改成NewUserForm便可,在瀏覽器點擊Register獲得以下界面,能夠看到email這同樣出現了:網站
參考連接:ui
[1] https://pythonprogramming.net/user-login-logout-django-tutorial/?completed=/messages-django-tutorial/url