小白学习django第五站-简易案例
首先在setting.py文件中编写数据库配置内容
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'site', 'USER': 'user', 'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': '3306', } }
之后在app中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}'
通过pycham的控制台输入
makemigrations appname
migrate appname
创建数据库中的表
之后进行模版渲染,在app中views.py中编写index, add, list, detail, edit这五个函数
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): # 添加页面 if request.method == 'GET': # 如果请求为get请求 return render(request, 'blog/demo_add.html') # 渲染 add 页面 elif request.method == 'POST': # 如果请求为post页面 title = request.POST.get('title') # 获取form表单填写的标题 content = request.POST.get('content') # 获取form表单填写的内容 blog = BlogModle2(title=title, content=content) # 数据库写入 blog.save() return render(request, 'blog/demo_add.html') # 渲染add页面
{% extends 'blog/demo_base.html' %} {% block title %} 添加博客 {% endblock %} {% block bodyblock %} <h1>添加新文章</h1> <form action="" method="POST"> {% csrf_token %} {# 防止跨域攻击与请求 #} {# <form action="" method="GET"> {% csrf_token %}#} 标题<input type="text" autocomplete="off" id="title" placeholder="请输入标题" 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): # 列表页面 blog_list = BlogModle2.objects.all() # 获取数据库表中所有内容 return render(request, 'blog/demo_list.html', context={'blog_list': blog_list}) # 传递blog_list内容,渲染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): # 详情页面 blog = BlogModle2.objects.get(id=blog_id) # 获取选中数据库表中标题那条数据 return render(request, 'blog/demo_detail.html', context={'blog': blog}) # 渲染detail页面
{% extends 'blog/demo_base.html' %} {% block title %} 文章详情 {% endblock %} {% block bodyblock %} <h1>{{ blog.title }}</h1> {{ blog.content }} {% endblock %}
delete
def blog_delete(request, blog_id): # 删除功能 blog = BlogModle2.objects.get(id=blog_id) # 获取选中数据库表中标题那条数据 if blog: # 如果存在 blog.delete() # 删除 return redirect(reverse('blog_list')) # 返回列表页面 else: return HttpResponse('不存在这条博客')
edit
def blog_edit(request, blog_id): # 编辑 blog = BlogModle2.objects.get(id=blog_id) # 获取选中数据库中数据 if request.method == 'GET': # 如果是GET请求 return render(request, 'blog/demo_add.html', context={'blog': blog}) # 渲染add页面,自动填入获取内容 elif request.method == 'POST': # 如果是post请求 blog.title = request.POST.get('title') # 标题修改 blog.content = request.POST.get('content') # 内容修改 blog.save() return redirect(reverse('blog_list')) # 重定向到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>