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 in</button>
<input type="hidden"