First, the following two red box of html copies to the corresponding location of the new project, be sure to position the same as (specific html files, save files in my blog column in the garden),
html file, appear similar to {% url "shop: login"%} place, remember, shop application to be replaced in the name of the actual project.
Two, settings set as follows.
shop: product_list representation after a successful login, jump to which path ( 'product_list /') url address.
shop: login which represents the landing path ( 'login /') url addresses.
Three, provided in accordance with the code below base.html
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% endblock %}</title> <link href="{% static "css/base.css" %}" rel="stylesheet"> </head> <body> <div id="header"> <a href="/" class="logo">My shop</a> </div> {% if request.user.is_authenticated %} <div id="subheader" style="margin-left: 20px"> <div class="cart"> Your cart is empty. </div> </div> {% endif %} <div style="margin-right: 20px"> {% if request.user.is_authenticated %} Hello {{ request.user.first_name }},<a href="{% url "shop:password_change" %}">修改密码</a> <a href="{% url "shop:logout" %}">Logout</a> {% else %} <a href="{% url "shop:login" %}">Log-in</a> {% endif %} </div> <div id="content"> {% block content%} {% endblock %} </div> </body> </html>
Fourth, in the bottom of the file in forms.py, paste the following code:
from django import forms from django.contrib.auth.models import User class LoginForm(forms.Form): username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput) class UserRegistrationForm(forms.ModelForm): password = forms.CharField(label='Password', widget=forms.PasswordInput) password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput) class Meta: model = User fields = ('username', 'first_name', 'email') def clean_password2(self): cd = self.cleaned_data if cd['password'] != cd['password2']: raise forms.ValidationError('Password don\'t match.') return cd['password2']
Fifth, in urls.py file below, paste the following code:
from django.urls import path from django.contrib.auth import views as auth_views from . import views app_name = 'shop' urlpatterns = [ path('login/', auth_views.LoginView.as_view(), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'), path('', views.product_list, name='product_list'), # change password urls path('password_change/', auth_views.PasswordChangeView.as_view( template_name='shop/password_change_form.html', success_url="/password_change/done/", ), name='password_change'), path('password_change/done/', auth_views.PasswordChangeDoneView.as_view( template_name='shop/password_change_done.html' ), name='password_change_done'), # reset password urls path('password-reset/', auth_views.PasswordResetView.as_view( template_name="shop/password_reset_form.html", email_template_name="shop/password_reset_email.html", subject_template_name="shop/password_reset_subject.txt", success_url="/password-reset-done/", ), name='password_reset'), path('password-reset-done/', auth_views.PasswordResetDoneView.as_view( template_name="shop/password_reset_done.html" ), name='password_reset_done'), path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view( template_name="shop/password_reset_confirm.html", success_url="/password-reset-complete/", ), name='password_reset_confirm'), path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view( template_name="shop/password_reset_complete.html" ), name='password_reset_complete'), path('register/', views.register, name='register'), ]
Sixth, under views.py file, paste the following code:
Import the render django.shortcuts from, get_object_or_404 from the Category .models Import, Product from cart.forms Import CartAddProductForm from django.contrib.auth.decorators Import login_required from .forms Import UserRegistrationForm DEF Register (Request): IF request.method == 'the POST ': user_form = UserRegistrationForm (request.POST) IF user_form.is_valid (): # create a new user object, but avoid to save it new_user = user_form.save (the commit = False) # settings selected password new_user.set_password (user_form .cleaned_data [ 'password']) # save the user object new_user.save () return render(request, 'shop/register_done.html', {'new_user': new_user}) else: user_form = UserRegistrationForm() return render(request, 'shop/register.html', {'user_form': user_form}) @login_required def product_list(request, category_slug=None): # ......... @login_required def product_detail(request, id, slug): # ........