在之前做的豆瓣读书查询基础上,添加一个登录、注册页面
1.将默认数据库配置为mysql,在DouBan/setting.py中进行修改
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'doubanDB', #数据库名,要提前建好
'USER': 'root',
'PASSWORD': '123456',
'HOST': 'localhost',
'PORT': 3306,
}
}
2.在DouBan/__init__.py中添加
import pymysql
pymysql.install_as_MySQLdb()
3.在BookList/models.py输入
class User(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
password = models.CharField(max_length=32)
def __str__(self):
return self.name
class Meta:
verbose_name = '用户'
verbose_name_plural = '用户'
4.添加一个DouBan.html,包含注册、登录功能
Home
5.设置css、js、img路径(在templates同级目录下创建static文件夹,在DouBan/settings.py中设置)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(os.path.join(BASE_DIR, 'static')),
)
6.修改DouBan/urls.py
from BookList.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('typeBook/',BookCrawlPage),
path('mainBook/',mainBookPage),
path('register/',register),
path('login/',login),
path('index/',index)
]
7.在manage.py同级目录下,进入cmd输入
python manage.py makemigrations
python manage.py migrate
8.查看mysql,是否生成
9. 修改BookList/views.py
from BookList.models import User
from django.shortcuts import redirect
def index(request):
render(request,'DouBan.html')
def login(request):
if request.method=='POST':
name = request.POST.get('username')
pwd = request.POST.get('pwd')
if User.objects.filter(name=name):
if User.objects.filter(name=name)[0].password==pwd:
return redirect('/mainBook/')
else:
return HttpResponse('密码错误')
else:
HttpResponse('用户不存在')
return render(request,'DouBan.html')
def register(request):
if request.method=='POST':
username = request.POST.get('username')
password = request.POST.get('pwd')
if User.objects.filter(name=username):
return HttpResponse('该账号已经注册')
else:
user_obj = User(name=username,password=password)
user_obj.save()
return HttpResponse('注册成功')
return render(request,'DouBan.html')
10.查看效果
11.查看数据库
12.创建超级用户管理页面,根据要求输入用户名、密码和邮箱
python manage.py createsuperuser
13.在BookList/admin.py注册模块
from BookList.models import User
admin.site.register(User)
14.登录管理页面 http://127.0.0.1/admin/
网页粒子特效背景 Particleground.js 的简单引入