zoukankan      html  css  js  c++  java
  • django框架学习(增删查改)

    MVC
    MVC,是模型(Model)-视图(View)-控制器(Controller)的缩写。其具体定义如下:
    M:模型(Model),数据存取层,负责业务对象和数据库对象。
    V:视图(View),与用户的交互,负责显示与怎样显示。
    C:控制器(Controller),接受用户动作,调用模型,输出相应视图。

    MTV
    Django的MTV设计模式是借鉴和遵循MVC的
    MTV具体定义如下:
    M:模型(Model),负责业务对象和数据库的关系映射。
    T:模板(Template),负责如何把页面展示给用户。
    V:视图(View),负责业务逻辑,并在适当时候调用模型和模板。

    创建 POST 表单(它具有修改数据的作用)需要小心跨站点请求伪造。
    所有针对内部 URL 的 POST 表单都应该使用 {% csrf_token %} 模板标签。

    models.py

    class Book(models.Model):
        #objects = models.Manager()
        name = models.CharField(max_length=200)
        author = models.CharField(max_length=200)
        pub_house =models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')

    views.py

    from django.shortcuts import render
    
    # Create your views here.
    from django.shortcuts import HttpResponse
    
    def index(request):
         return HttpResponse('ok')
    
    from .models import Book
    from django.http import HttpResponseRedirect
    from django.urls import reverse
    
    def detail(request):
        book_list = Book.objects.order_by('pub_date')[:5]#查询数据不超过5条
        context = {'book_list': book_list}
        return render(request, 'detail.html', context)
    
    def addBook(request):
        if request.method == 'POST':
            temp_name = request.POST['name']
            temp_author = request.POST['author']
            temp_pub_house = request.POST['pub_house']
    
        from django.utils import timezone#获取当前时间用
        temp_book = Book(name=temp_name, author=temp_author, pub_house=temp_pub_house, pub_date=timezone.now())
        temp_book.save()#保存数据
    
        #重定向
        return HttpResponseRedirect(reverse('detail'))
    
    def delBook(request,book_id):
        bookid=book_id
        Book.objects.filter(id=bookid).delete()#删除数据
        return  HttpResponseRedirect(reverse('detail'))

    urls.py

        path('detail/', detail, name='detail'),
        path('addBook/', addBook, name='addBook'),
        path('delBook/<int:book_id>/', delBook, name='delBook'

    detail.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    
    <body>
    <h1>Book List</h1>
    <table>
        <tr>
            <td>编号ID</td>
            <td>书名</td>
            <td>作者</td>
            <td>出版社</td>
            <td>出版时间</td>
            <td>操作</td>
        </tr>
    {% for book in book_list.all %}
        <tr>
            <td>{{ book.id }}</td>
            <td>{{ book.name }}</td>
            <td>{{ book.author }}</td>
            <td>{{ book.pub_house }}</td>
            <td>{{ book.pub_date }}</td>
            <td><a href="{% url 'delBook' book.id %}">删除</a></td>
        </tr>
    {% endfor %}
    </table>
    
    <form action="{% url 'addBook' %}" method="post" name="addBook">
        {% csrf_token %}
        <p><span>书名:</span><input type="text"  name="name"></p>
        <p><span>作者:</span><input type="text"  name="author"></p>
        <p><span>出版社:</span><input type="text"  name="pub_house"></p>
        <input type="submit" value="添加">
    </form>
    
    </body>
    </html>
  • 相关阅读:
    Shell IFS
    Crontab
    linux awk
    free
    条件语句练习2
    条件语句练习
    打印菜单
    条件测试语法
    read 命令
    jQuery(实例)
  • 原文地址:https://www.cnblogs.com/yinwenbin/p/11831247.html
Copyright © 2011-2022 走看看