一、创建用户账户
将建立一个用户注册和身份验证系统,让用户能够注册账户,进而登录和注销。我们将创建一个新的应用程序,其中包含与处理用户账户相关的所有功能。
创建user
我们首先使用命令 startapp 来创建一个名为 users 的应用程序:
python manage.py startapp users
成功后,出现如图文件
修改settings.py
修改项目根目录中的urls.py
path(r'^users/', include('users.urls', namespace='users')),
为用户加入登录,注销,注册页面
编写users/urls.py
"""为应用程序users定义URL模式""" from django.urls import path #从当前的urls.py模块所在的文件夹中导入视图 #变量urlpatterns是一个列表,包含可在应用程序learning_logs中请求的网页 app_name = 'users' from django.contrib import admin from django.urls import path, include from django.contrib.auth.views import LoginView from . import views urlpatterns = [ # 登录页面 path(r'^login/$',LoginView.as_view(template_name='users/login.html'), name='login'), # 注销 path(r'^logout/$', views.logout_view, name='logout'), # 注册页面 path(r'^register/$', views.register, name='register'), ]
编写users/views.py
from django.shortcuts import render from django.http import HttpResponseRedirect from django.urls import reverse from django.contrib.auth import logout,login,authenticate from django.contrib.auth.forms import UserCreationForm # Create your views here. def logout_view(request): """注销用户""" logout(request) return HttpResponseRedirect(reverse('learning_logs:index')) def register(request): """注册新用户""" if request.method != 'POST': # 显示空的注册表单 form = UserCreationForm() else: # 处理填写好的表单 form = UserCreationForm(data=request.POST) if form.is_valid(): new_user = form.save() # 让用户自动登录,再重定向到主页 authenticated_user = authenticate(username=new_user.username, password=request.POST['password1']) login(request, authenticated_user) return HttpResponseRedirect(reverse('learning_logs:index')) context = {'form': form} return render(request, 'users/register.html', context)
编写模板
在users文件下建立如图文件结构
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% extends "learning_logs/base.html" %} {% block content %} {% if form.errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} <form method="post" action="{% url 'users:login' %}"> {% csrf_token %} {{ form.as_p }} <button name="submit">log in</button> <input type="hidden" name="next" value="{% url 'learning_logs:index' %}" /> </form> {% endblock content %} </body> </html>
register.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% extends "learning_logs/base.html" %} {% block content %} <form method="post" action="{% url 'users:register' %}"> {% csrf_token %} {{ form.as_p }} <button name="submit">register</button> <input type="hidden" name="next" value="{% url 'learning_logs:index' %}" /> </form> {% endblock content %} </body> </html>
链接到登录页面
修改base.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p> <a href="{% url 'learning_logs:index' %}">Learning Log</a> - <a href="{% url 'learning_logs:topics' %}">Topics</a> {% if user.is_authenticated %} Hello, {{ user.username }}. <a href="{% url 'users:logout' %}">log out</a> {% else %} <a href="{% url 'users:register' %}">register</a> - <a href="{% url 'users:login' %}">log in</a> {% endif %} </p> {% block content %}{% endblock content %} </body> </html>
查看效果
首页
注册页
登录页
登录后
二、用户拥有自己的信息
使用 @login_required 限制访问
限制对topics 页面的访问
每个主题都归特定用户所有,因此应只允许已登录的用户请求 topics 页面。
修改learning_logs/views.py
from django.shortcuts import render from django.http import HttpResponseRedirect,Http404 from django.urls import reverse from django.contrib.auth.decorators import login_required from .models import Topic, Entry from .forms import TopicForm, EntryForm # Create your views here. def index(request): """学习笔记的主页""" return render(request, 'learning_logs/index.html') @login_required def topics(request): """显示所有的主题""" topics = Topic.objects.filter(owner=request.user).order_by('date_added') context = {'topics': topics} return render(request, 'learning_logs/topics.html', context) @login_required def topic(request, topic_id): """显示单个主题及其所有的条目""" topic = Topic.objects.get(id=topic_id) # 确认请求的主题属于当前用户 if topic.owner != request.user: raise Http404 entries = topic.entry_set.order_by('-date_added') context = {'topic': topic, 'entries': entries} return render(request, 'learning_logs/topic.html', context) @login_required def new_topic(request): """添加新主题""" if request.method != 'POST': # 未提交数据:创建一个新表单 form = TopicForm() else: # POST提交的数据,对数据进行处理 form = TopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.owner = request.user new_topic.save() return HttpResponseRedirect(reverse('learning_logs:topics')) context = {'form': form} return render(request, 'learning_logs/new_topic.html', context) @login_required def new_entry(request, topic_id): """在特定的主题中添加新条目""" topic = Topic.objects.get(id=topic_id) if request.method != 'POST': # 未提交数据,创建一个空表单 form = EntryForm() else: # POST提交的数据,对数据进行处理 form = EntryForm(data=request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return HttpResponseRedirect(reverse('learning_logs:topic', args=[topic_id])) context = {'topic': topic, 'form': form} return render(request, 'learning_logs/new_entry.html', context) @login_required def edit_entry(request, entry_id): """编辑既有条目""" entry = Entry.objects.get(id=entry_id) topic = entry.topic if topic.owner != request.user: raise Http404 if request.method != 'POST': # 初次请求,使用当前条目填充表单 form = EntryForm(instance=entry) else: # POST提交的数据,对数据进行处理 form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('learning_logs:topic', args=[topic.id])) context = {'entry': entry, 'topic': topic, 'form': form} return render(request, 'learning_logs/edit_entry.html', context)
为实现这种重定向,修改settings.py
在最后加入一行代码:
# 我的设置 LOGIN_URL = '/users/login/'
将数据关联到用户
修改模型 Topic
对models.py进行修改
from django.db import models from django.contrib.auth.models import User # Create your models here. class Topic(models.Model): # """用户学习的主题""" text = models.CharField(max_length=200) date_added = models.DateTimeField(auto_now_add=True) owner = models.ForeignKey(User,on_delete=models.CASCADE) def __str__(self): # """返回模型的字符串表示""" return self.text class Entry(models.Model): """学到的有关某个主题的具体知识""" topic = models.ForeignKey(Topic, on_delete=models.CASCADE) text = models.TextField() date_added = models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural = 'entries' def __str__(self): """返回模型的字符串表示""" return self.text[:50] + "..."
在控制台确定当前有哪些用户
再确定用户id
迁移数据库
为验证迁移符合预期,可在 shell 会话中像下面这样做:
查看效果
登录账号为11_admin时,数据库的内容都显示
登录账号为其它时,内容为空,需要添加内容