1. 用户注册和身份验证
1.创建另一个应用程序users
在项目中新建users
python manage.py startapp users
添加应用到settings.py中的INSTALLED_APPS列表中
INSTALLED_APPS = [
--snip--
# my apps
'learning_logs',
'users',
]
2.添加url
修项目根目录的url.py,添加users的url
path('users/', include('users.urls', namespace='users')),
2. 登录
1.在应用users目录下新建urls.py
"""define url mode for app users"""
from django.urls import path
from django.contrib.auth.views import login
from . import views
urlpatterns = [
# login page
path('login/', login, {'template_name': 'users/login.html'},
name='login'),
]
app_name = 'users'
注意添加app_name = 'users'
2.创建模板login.html
新建learning_log/users/templates/users/login.html
{% extends "learning_logs/base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.p>
{% endif %}
<form action="{% url 'users:login' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">log inbutton>
<input type="hidden" name="next"
value="{% url 'learning_logs:index' %}">
form>
{% endblock content %}
3.在base.html添加条件
<p>
<a href="{% url 'learning_logs:index' %}">Learning Loga> -
<a href="{% url 'learning_logs:topics' %}">Topicsa> -
{% if user.is_authenticated %}
Hello, {{ user.username }}.
{% else %}
<a href="{% url 'users:login' %}">log ina>
{% endif %}
p>
3. 注销
1.注销登录
在users/urls.py中添加
# logout
path('logout/', views.logout_view, name='logout'),
2.编写视图函数logout_view()
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth import logout
# Create your views here.
def logout_view(request):
"""logout"""
logout(request)
return HttpResponseRedirect(reverse('learning_logs:index'))
3.添加注销链接在base.thml
<p>
<a href="{% url 'learning_logs:index' %}">Learning Loga> -
<a href="{% url 'learning_logs:topics' %}">Topicsa> -
{% if user.is_authenticated %}
Hello, {{ user.username }}.
<a href="{% url 'users:logout' %}">log outa>
{% else %}
<a href="{% url 'users:login' %}">log ina>
{% endif %}
p>
4. 注册
1.添加url规则到users/urls.py
# sign up page
path('register/', views.register, name='register'),
2.视图函数register()
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.forms import UserCreationForm
# Create your views here.
def logout_view(request):
"""logout"""
logout(request)
return HttpResponseRedirect(reverse('learning_logs:index'))
def register(request):
"""sign up a new user"""
if request.method != 'POST':
# show an empty form
form = UserCreationForm()
else:
# process the completed form
form = UserCreationForm(data=request.POST)
if form.is_valid():
new_user = form.save()
# let the user sign in; redirect to index
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)
3.编写注册页面模板register.html
{% extends "learning_logs/base.html" %}
{% block content %}
<form action="{% url 'users:register' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">registerbutton>
<input type="hidden" name="next" value="{% url 'learning_logs:index' %}">
form>
{% endblock content %}
4.在base.html添加链接到注册页面
<p>
<a href="{% url 'learning_logs:index' %}">Learning Loga> -
<a href="{% url 'learning_logs:topics' %}">Topicsa> -
{% if user.is_authenticated %}
Hello, {{ user.username }}.
<a href="{% url 'users:logout' %}">log outa>
{% else %}
<a href="{% url 'users:register' %}">registera> -
<a href="{% url 'users:login' %}">log ina>
{% endif %}
p>