#settings
userinfo
bookstore
os.path.join(BASE_DIR,'templates')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'ssq',
'USER': 'root',
'PASSWORD': '1234567',
'HOST': '127.0.0.1',
'PORT': 3306
}
}
#主路由
#init
import pymysql
pymysql.install_as_MySQLdb()
#urls
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
# path('bookstore/', include('bookstore.urls')),
path('userinfo/', include('userinfo.urls'))
]
#分路由 userinfo
#templates
#login
#register
#models
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField('姓名', max_length=20, unique=True)
password = models.CharField('密码', max_length=10)
#urls
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('login', views.login),
path('reg', views.register),
]
#views
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from . import models
# Create your views here.
def register(request):
if request.method == 'GET':
return render(request, 'register.html')
elif request.method == 'POST':
username = request.POST.get('username', '')
password = request.POST.get('password', '')
password1 = request.POST.get('password1', '')
if username == '':
username.error = '用户名不能为空'
return render(request, 'register.html', locals())
elif password == '':
password.error = '密码不能为空'
return render(request, 'register.html', locals())
elif password != password1:
return HttpResponse('两次密码不一样')
try:
user = models.User.objects.create(
name=username,
password=password
)
return HttpResponse('注册成功,进人登录')
except:
return HttpResponse('注册失败')
def login(request):
if request.method == 'GET':
return render(request, 'login.html', locals())
elif request.method == 'POST':
username = request.POST.get('username', '')
remember = request.POST.get('remember', '')
password = request.POST.get('password', '')
try:
user = models.User.objects.get(name=username,
password=password)
return HttpResponse('登录成功,返回添加图书页面')
except:
return HttpResponse('登录失败')
#分路由 bookstore
#templates
#add
返回首页
#homepage
{% csrf_token %}
添加图书
显示图书
#mod
#show
返回添加页面
#models
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField('书名', max_length=50)
pub_house = models.CharField('出版社', max_length=200)
comment = models.CharField('评论量', max_length=200)
read = models.CharField('阅读量', max_length=200)
#urls
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.homepage),
path('add', views.add),
path('show', views.show),
path('mod/
path('del/
path('search', views.search)
]
#views
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from . import models
# Create your views here.
def homepage(request):
return render(request, '../templates/homepage.html')
def add(request):
if request.method == 'GET':
return render(request, '../templates/add.html')
elif request.method == 'POST':
title = request.POST.get('title', '')
pub_house = request.POST.get('pub_house', '')
comment = request.POST.get('comment', '')
read = request.POST.get('read', '')
abook = models.Book.objects.create(
title=title,
pub_house=pub_house,
comment=comment,
read=read,
)
return HttpResponse('添加成功,返回')
def show(request):
abook = models.Book.objects.all()
return render(request, '../templates/show.html', locals())
def modbook(request, i_id):
try:
abook = models.Book.objects.get(id=i_id)
except:
return HttpResponse('没有找到!!!!!')
if request.method == 'GET':
return render(request, '../templates/mod.html', locals())
elif request.method == 'POST':
try:
pub_house = request.POST.get('pub_house', '')
abook.pub_house = pub_house
abook.save()
return HttpResponse(' 修改成功返回首页')
except:
return HttpResponse('修改失败')
def delbook(request, i_id):
try:
abook = models.Book.objects.get(id=i_id)
abook.delete()
return HttpResponseRedirect('/bookstore/show')
except:
return HttpResponse('没有找到!!!!!')
def search(requset):
title = requset.GET.get("search")
all_book = models.Book.objects.filter(title__contains=title)
return render(requset, '../templates/show.html', locals())



