作者: zifanwang 发布于2020-05-12
首先命令行cd进manage.py同级目录下创建用户app:
python manage.py startapp userprofile
Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>cd..
C:\Users>cd..
C:\>cd mysite
C:\mysite>python manage.py startapp userprofile
C:\mysite>
多了一个文件夹,这就是我们创建的用户app 用户登陆时需要填写用户名和密码等,因此需要用到Form表单类 在userprofile中新建表单类文件froms.py并输入:
from django import forms
from django.contrib.auth.models import User
class UserLoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField()
接着编写视图,在userprofile/views.py中写视图函数:
from django.contrib.auth import authenticate, login
from django.http import HttpResponse
from django.shortcuts import render
from userprofile.froms import UserLoginForm
def user_login(request):
if request.method == "POST":
user_login_form = UserLoginForm(data=request.POST)
if user_login_form.is_valid():
# .cleaned_data洗出合法数据
data = user_login_form.cleaned_data
# 检验账号、密码是否正确匹配数据库中的某个用户
# 如果均匹配则返回这个user对象
user = authenticate(username=data['username'], password=data['password'])
if user:
login(request, user)
return redirect("article:article_list")
else:
return HttpResponse("账号或密码输入有误,请重新输入")
else:
return HttpResponse("账号或密码输入不合法")
elif request.method =="GET":
user_login_form = UserLoginForm()
context = {'from': user_login_form}
return render(request, 'userprofile/login.html', context)
else:
return HttpResponse("请使用GET或POST请求数据")
接着在templates文件夹里创建userprofile文件夹,再在里面创建login.html并写入:
{% extends "base.html" %}
{% load static %}
{% block title %}
Login
{% endblock title %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-12">
<br>
<form method="post" action=".">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
</div>
</div>
{% endblock content %}
接下来在header.html中增加登陆按钮:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">My Site</a>
<div>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'article:article_list' %}">Article</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'article:article_create' %}">New</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ user.username }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Exit</a>
</div>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'userprofile:login' %}">Login</a>
</li>
{% endif %}
</ul>
</div>
</div>
</nav>
接着在mysite/userprofile创建urls.py并写入:
from django.urls import path
from . import views
app_name = 'userprofile'
urlpatterns=[
path('login/', views.user_login, name='login'),
]
在mysite/urls.py里增加:
path('userprofile/', include('userprofile.urls', namespace='userprofile')),
接着配置mysite/settings.py中注册app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'article',
'userprofile',
]
再接着运行服务器(python manage.py runserver):
可以看见登陆界面正常显示了,我们也可以登陆了