栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

图书管理第四

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

图书管理第四

#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



   
    登录



    {% csrf_token %}
   


        姓名:
        {{ username.error }}
   


   

密码:


   

记住密码:


   


#register



   
    注册



    {% csrf_token %}
   

姓名:
        {{ username.error }}
   


   

密码:
        {{ password.error }}
   


   

确认密码:


   


#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



   
    添加图书


返回首页


     {% csrf_token %}
   
       
   
   
       
   
   
       
   
   
       
   
       
           
       




 

#homepage



   
    首页

{% csrf_token %}

添加图书


显示图书


#mod



   
    修改



    {% csrf_token %}
   
        书名:{{ abook.title }}
   
   
       
   
   
        评论量:{{ abook.comment }}
   

   
        阅读量:{{ abook.read }}
   
   
       
   



 

#show



   
    显示图书


返回添加页面


   
   



    {% csrf_token %}
   
       
           
           
           
           
           
           
       
        {% for i in abook %}
       
           
           
           
           
           
           
       
        {% endfor %}
        {% for i in all_book %}
       
           
           
           
           
           
           
       
        {% endfor %}


   

书名出版社评论量阅读量修改删除
{{ i.title }}{{ i.pub_house }}{{ i.comment }}{{ i.read }}{ i.id }}">修改{ i.id }}">删除
{{ i.title }}{{ i.pub_house }}{{ i.comment }}{{ i.read }}{ i.id }}">修改{ i.id }}">删除




#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/', views.modbook),
    path('del/', views.delbook),
    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())
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号