1.配置urls.py
from django.contrib import admin
from django.urls import path,re_path
from sign import views
urlpatterns = [
path('admin/', admin.site.urls),
# re_path 等于url
re_path(r'index/', views.index), # 登录页
re_path('^$', views.index), #登录页
re_path(r'^accounts/login/$', views.index), #登录页(强制登录跳转)
re_path(r'^login_action/$', views.login_action), #登录处理
re_path(r'^event_manage/$', views.event_manage) #首页
]
2.配置视图 views.py
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required
# Create your views here.
def index(request):
# return HttpResponse("hello world!")
return render(request, 'index.html')
def login_action(request):
if request.method == 'POST':
# 根据name属性获取input值
username = request.POST.get('username','')
password = request.POST.get('password','')
# 验证账号密码是否存在数据库,如果不存在返回None
user = auth.authenticate(username=username,password=password)
if user is not None:
# 登录
auth.login(request,user)
# 保存session到浏览器
request.session['user'] = username
# 登录成功后跳转到 首页
response = HttpResponseRedirect('/event_manage/')
return response
else:
return render(request, 'index.html', {'error':'账号或密码错误!'})
@login_required
def event_manage(request):
# 获取session
username = request.session.get('user', '')
return render(request,'event_manage.html',{'user':username})
3.配置模板 templates
3.1 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>发布会管理系统</title>
</head>
<body>
<h1>发布会管理系统</h1>
<form method="post" action="/login_action/">
<input name="username" type="text" placeholder="账号"><br>
<input name="password" type="text" placeholder="密码"><br>
{{ error }}<br> # 获取2 当账号密码错误时的错误提示
<button id="submit" type="submit">登陆</button>
</form>
</body>
</html>
3.2 event_manage.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>发布会管理</title>
</head>
<body>
<h1>HI! {{user}} </h1>
<h1>Welcome!</h1>
</body>
</html>
4.使用数据库登录系统,创建Event,Guest表
4.1 创建超级管理员
根据指令来显示
e:guest>python manage.py createsuperuser
4.2 创建Event,Guest表
from django.db import models
# Create your models here.
# 发布会表
class Event(models.Model):
name = models.CharField(max_length=100)
limit = models.IntegerField
status = models.BooleanField
address = models.CharField(max_length=200)
start_time = models.DateTimeField('events time')
create_time = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
# 嘉宾表
class Guest(models.Model):
event = models.ForeignKey(Event,on_delete=models.CASCADE)
realname = models.CharField(max_length=64)
phone = models.CharField(max_length=16)
email = models.EmailField
sign = models.BooleanField
create_time = models.DateTimeField(auto_now=True)
class Meta:
# model内部类
unique_together = ('event','phone')
def __str__(self):
return self.realname
4.3 配置admin.py
from django.contrib import admin
from sign.models import Event,Guest
# Register your models here.
class EventAdmin(admin.ModelAdmin):
list_display = ['id','name','status','address','start_time']
search_fields = ['name']
# list_filter = ['status']
class GuestAdmin(admin.ModelAdmin):
list_display = ['realname','phone','email','sign','create_time','event']
search_fields = ['realname','phone']
# list_filter = ['sign']
admin.site.register(Event,EventAdmin)
admin.site.register(Guest,GuestAdmin)
4.4 数据库迁移
e:guest>python manage.py makemigrations sign
e:guest>python manage.py migrate
5.结果
登录失败
登录成功