Django 09 博客小案例

Django 09 博客小案例

  urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('index/',views.index,name='blog_index'),
    path('add/',views.add,name='blog_add'),
    path('list/',views.list,name='blog_list'),
    path('detail/<blog_id>/',views.detail,name='blog_detail'),
    path('edit/<blog_id>/',views.edit,name='blog_edit'),
    path('delete/<blog_id>/',views.delete,name='blog_delete'),
    ]

  views.py

from django.shortcuts import render,redirect,reverse

# Create your views here.
from .models import Blog
from django.http import HttpResponse


def index(request):
    '''
    主页
    '''
    return render(request,'blog/demo_index.html')

def add(request):
    '''
    添加页
    '''
    if request.method == 'GET': #若未点击添加,则进入这个页面的时候method就是GET
        return render(request,'blog/demo_add.html')
    elif request.method == 'POST':   #若点击添加,method变成POST,就可以获取POST中的添加信息(queryset)
        title = request.POST.get('title')  #赋值title
        content = request.POST.get('content') #赋值content
        if title == '' and content == '':  #如果title和content都为空,则重新输入内容,不传入数据库
            return redirect(reverse('blog_add'))
        else: #否则传入数据库
            blog=Blog(title=title,content=content)
            blog.save()
            return redirect(reverse('blog_add'))
    else:
        return HttpResponse('这不是一个有效的')

def list(request):
    '''
    列表页
    '''
    blog_list = Blog.objects.all() #从数据库中获取表的所有数据
    return render(request,'blog/demo_list.html',
                  context={'blog_list':blog_list, #传入模板
                           })

def detail(request,blog_id):
    '''
    详情页
    '''
    blog = Blog.objects.get(id=blog_id)  #根据获取的id来查询这条数据
    return render(request,'blog/demo_detail.html',
                  context={'blog':blog})    #传入模板

def edit(request,blog_id):
    '''
    修改页
    '''
    if request.method == 'GET': #如果为点击修改,method为GET
        blog = Blog.objects.get(id = blog_id)
        return render(request,'blog/demo_edit.html',
                      context={
                          'blog':blog, #将该页内容传入修改框内
                      })
    elif request.method == 'POST': #若以点击修改,则method变为POST
        content = request.POST.get('content')  #获取POST中的content
        title = request.POST.get('title') #获取POST中的title
        if content == '' and title == '': #如果content和title同时为空,则不传入数据库
            return render(request,'blog/demo_edit.html')
        else: #否则,覆盖原内容,传入数据库
            blog = Blog.objects.get(id = blog_id)
            blog.title = title
            blog.content = content
            blog.save()
            return redirect(reverse('blog_list'))
    else:
        return HttpResponse('这不是一个有效的')



def delete(request,blog_id):
    '''
    删除页
    '''
    blog = Blog.objects.get(id=blog_id)
    if blog: #如果blog存在,则删除
        blog.delete()
        return redirect(reverse('blog_list'))
    else:
        return HttpResponse('没有这篇博客')

  add模板(添加页)

{% extends 'blog/demo_base.html' %}
{% block title %}
    添加博客
{% endblock %}
{% block bodyblock %}
    <h1>添加新文章</h1>
    <form action="" method="POST"> {% csrf_token %} <!csrf是为了防止跨域攻击的,加上就行;POST中存储你所输入的内容(title,content)>
        标题<input type="text" autocomplete="off" id="title"
                 placeholder="请输入标题" name='title'> <br> <br><br>
        内容 <textarea name="content" id="content"
                     placeholder="请输入内容" cols="30" rows="10"></textarea>
        <button type="submit">发布博客</button>
    </form>
{% endblock %}

  base模板(基页)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}
    
    {% endblock %}</title>
</head>
<body>
{% block bodyblock %}

{% endblock %}

</body>
</html>

  detail模板(详情页)

{% extends 'blog/demo_base.html' %}
{% block title %}
    文章详情
{% endblock %}
{% block bodyblock %}
    <h1>文章标题:{{ blog.title }}</h1>
    文章内容 <br>
    {{ blog.content }}
{% endblock %}

  edit模板 (修改页)

{% extends 'blog/demo_base.html' %}
{% block title %}
    修改博客
{% endblock %}
{% block bodyblock %}
    <h1>修改文章</h1>
    <form action="" method="POST"> {% csrf_token %}
        标题<input type="text" autocomplete="off" id="title"
                 placeholder="请输入标题" name='title' value="{{ blog.title }}"> <br> <br><br> <! 原题目写在前面的value里面>
        内容 <textarea name="content" id="content"
                     placeholder="请输入内容" cols="30" rows="10">{{ blog.content }}</textarea> <!原内容写在textarea标签内>
        <button type="submit">修改博客</button>
    </form>
{% endblock %}

  index模板(主页)

{% extends 'blog/demo_base.html' %}

{% block title %}
    首页
{% endblock %}

{% block bodyblock %}
    <tr>
        <td><a href="{% url 'blog_add' %}">添加文章</a></td>   <!a标签实现跳转,url为前面add的name>
        <td><a href="{% url 'blog_list' %}">文章列表</a></td>
    </tr>
{% endblock %}

  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>   <!因为跳转的是指定的内容,所以必须加上blog.id>
            <th><a href="{% url 'blog_edit' blog.id %}">编辑</a> |
                <a href="{% url 'blog_delete' blog.id %}">删除 </a></th>
        </tr>
    {% endfor %}

    </tbody>
    </table>


{% endblock %}

  

原文地址:https://www.cnblogs.com/xuchengcheng1215/p/9405515.html