from django.db import models
# Create your models here.
class Userinfo(models.Model):
email=models.CharField(max_length=50,unique=True)
password=models.CharField(max_length=50)
nickname=models.CharField(max_length=50)
mobile=models.CharField(max_length=32,unique=True)
birthday=models.DateField()
createtime=models.DateTimeField(auto_now_add=True)
class MessageInfo(models.Model):
message=models.CharField(max_length=2000)
score=models.DecimalField(max_digits=19,decimal_places=2,default=2)
messagetime=models.DateTimeField(auto_now_add=True)
useful=models.IntegerField(default=0)
useless=models.IntegerField(default=0)
uid=models.ForeignKey('Userinfo',on_delete=models.CASCADE)
from django.urls import path
from . import views
urlpatterns=[
path('index/',views.index),
path('signin/',views.signin),
path('find/',views.find),
path('addUser/',views.addUser),
path('login/',views.login),
path('logout/',views.logout),
path('review/',views.review),
path('comment/',views.comment)
]
from django.shortcuts import render,redirect
from .models import *
from django.contrib import messages
# Create your views here.
#打开主页面
def index(request):
return render(request,'index.html')
#打开登录页面
def signin(request):
return render(request,'signin.html')
#打开查找页面
def find(request):
return render(request,'find.html')
#注销
def logout(request):
del request.session['id']
return render(request,'index.html')
#打开评分页面
def review(request):
return render(request,'review.html')
#增加用户
def addUser(request):
try:
post=request.POST
user=Userinfo()
user.email=post.get('email')
existemail=Userinfo.objects.filter(email=user.email) #得到一个email=user.email的set集合
if len(existemail)>0: #通过set集合长度来判断是否已经存在
return render(request,'find.html',{'msg':'exist'}) #如果大于0,则此email已经存在,不能重复注册,所以重新渲染此页面并给出错误提示
else:
user.password=post.get('password')
user.nickname=post.get('nickname')
user.mobile=post.get('mobile')
existmobile=Userinfo.objects.filter(mobile=user.mobile)
if len(existmobile)>0:
return render(request,'find.html',{'msg1':'exist'})
else:
user.birthday=post.get('birthday')
user.save() #自动生成mysql语句插入数据
return redirect('/file/index')
except:
return render(request,'find.html')
def login(request):
post=request.POST
uinfo=post.get('info')
passwd=post.get('password')
# users=Userinfo.objects.filter(email=uinfo ,password=passwd)
# if len(users)>0:
# request.session['id']=users[0].email
# return redirect('/file/index')
#
# else:
# return render(request,'signin.html',{'message':'密码或者邮箱为空/输入错误!'})
email=Userinfo.objects.filter(email=uinfo)
if len(email)==1:
request.session['id']=email[0].email
passwd=Userinfo.objects.filter(password=passwd)
if len(passwd)!=0:
return redirect('/file/index')
# return render(request,'index.html',{'success':'1'}) #可以给一个页面传值
else:
return render(request,'signin.html',{'message':'密码错误!'})
else:
return render(request,'signin.html',{'message':'邮箱输入错误!'})
# infos=Userinfo.objects.all()
# flag=1
# for i in infos:
# if uinfo==i.email or uinfo==i.mobile and passwd==i.password:
# return redirect('/file/index')
# flag=0
# if flag==1:
# return render(request,'signin.html')
#发表评论
def comment(request):
try:
post=request.POST
msg=MessageInfo()
msg.message=post.get('message')
msg.score=post.get('score')
#取到用户id是通过前面存在的email来反查的,同时规定uid的值存的是对象,而不是id值
# email=request.session['id']
# user=Userinfo.objects.filter(email=email)
# msg.uid=user[0]
msg.uid=Userinfo.objects.filter(email=request.session['id'])[0]
msg.save()
# return render(request,'index.html')
return redirect('/file/index')
except:
return render(request,'review.html')
网友评论