First, write the contents of the database configuration file in setting.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'site', 'USER': 'user', 'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': '3306', } }
After writing model mapping table in the app file in models.py
from django.db import models # Create your models here. class BlogModle2(models.Model): title = models.CharField(max_length=50) # 标题 content = models.TextField() # 内容 def __str__(self): return f'title={self.title}, content={self.content}'
By pycham console input
makemigrations appname
migrate appname
Create a database table
After the template rendering index in views.py app written in, add, list, detail, edit the five functions
index
from django.shortcuts import render, redirect, reverse from django.http import HttpResponse from .models import BlogModle2 # Create your views here. def blog_index(request): # 主页函数 return render(request, 'blog/demo_index.html') # 渲染index页面
{% extends 'blog/demo_base.html' %} {% block title %} 首页 {% endblock %} {% block bodyblock %} <tr> <td><a href="{% url 'blog_add' %}">添加文章</a></td> <td><a href="{% url 'blog_list' %}">文章列表</a></td> </tr> {% endblock %}
add
DEF blog_add (Request): # Add a page IF request.method == ' GET ' : # If the request is a request to get return the render (Request, ' Blog / demo_add.html ' ) # Rendering page add elif request.method == ' POST ' : # If the request for the post page title = request.POST.get ( ' title ' ) # get the form filled out form title Content = request.POST.get ( 'Content ' ) # get form form filler content Blog = BlogModle2 (title = title, Content = Content) # database writes blog.save () return the render (Request, ' Blog / demo_add.html ' ) # rendering add page
The extends% { 'Blog / demo_base.html'%} {%}% Block title add blog {% endblock%} {%}% Block bodyblock < h1 of > add new articles </ h1 of > < form Action = "" Method = " the POST " > {%} {#% csrf_token attack and to prevent cross-domain request} # {# < form Action =" " Method =" the GET " > {%} # csrf_token%} of the title < INPUT type =" text " AutoComplete =" OFF " the above mentioned id =" title " placeholder = "Please enter a title." name='title' value="{{ blog.title }}"> <br> <br><br> 内容 <textarea name="content" id="content" placeholder="请输入内容" cols="30" rows="10">{{ blog.content }}</textarea> <button type="submit">发布博客</button> </form> {% endblock %}
list
DEF blog_list (Request): # List page blog_list BlogModle2.objects.all = () # Get all the content database table return the render (Request, ' Blog / demo_list.html ' , context = { ' blog_list ' : blog_list}) # pass blog_list content, rendering the page list
{% extends 'blog/demo_base.html' %} {% block title %} 文章列表 {% endblock %} {% block bodyblock %} <h1 style="margin-left: 100px">文章列表</h1> <table width="400px"> <thead style="font-size:20px"> <tr> <th>标题</th> <th>操作</th> </tr> </thead> <tbody> {% for blog in blog_list %} <tr> <th><a href="{% url 'blog_detail' blog.id %}">{{ blog.title }}</a></th> <th><a href="{% url 'blog_edit' blog.id %}">编辑</a> | <a href="{% url 'blog_delete' blog.id %}">删除 </a></th> </tr> {% endfor %} </tbody> </table> {% endblock %}
detail
DEF blog_detail (Request, blog_id): # Details page Blog = BlogModle2.objects.get (ID = blog_id) # Get the selected piece of data in a database table header return the render (Request, ' Blog / demo_detail.html ' , context = { ' Blog ' : Blog}) # rendering detail page
{% extends 'blog/demo_base.html' %} {% block title %} 文章详情 {% endblock %} {% block bodyblock %} <h1>{{ blog.title }}</h1> {{ blog.content }} {% endblock %}
delete
DEF blog_delete (Request, blog_id): # Remove Features Blog = BlogModle2.objects.get (the above mentioned id = blog_id) # get the selected piece of data in a database table titled IF Blog: # If there blog.delete () # delete return redirect (Reverse ( ' blog_list ' )) # returns a list of pages the else : return HttpResponse ( ' does not exist this blog ' )
edit
DEF blog_edit (Request, blog_id): # edit Blog = BlogModle2.objects.get (ID = blog_id) # Get the selected data in the database IF request.method == ' GET ' : # If GET request return the render (Request, ' Blog /demo_add.html ' , context = { ' Blog ' : Blog}) # rendering add page, acquires the content autofill elif request.method == ' the pOST ' : # If the request is a post blog.title = request.POST.get ( 'title ' ) # title modified blog.content = request.POST.get ( ' Content ' ) # content modification blog.save () return the redirect (Reverse ( ' blog_list ' )) # redirected to a page list
urls.py
from django.urls import path, re_path from . import views urlpatterns = [ path('index/', views.blog_index, name='blog_index'), path('add/', views.blog_add, name='blog_add'), path('list/', views.blog_list, name='blog_list'), path('detail/<blog_id>', views.blog_detail, name='blog_detail'), path('delete/<blog_id>', views.blog_delete, name='blog_delete'), path('edit/<blog_id>', views.blog_edit, name='blog_edit'), ]
base.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %} {% endblock %}</title> </head> <body> {% block bodyblock %} {% endblock %} </body> </html>