美文网首页
Django博客小案列

Django博客小案列

作者: 昆仑草莽 | 来源:发表于2019-07-05 14:23 被阅读0次

    前面我们以及知道了django的模板,变量,模型,表关联,这一章节学习一个博客小案列:
    首先建立渲染的页面:
    1、基础页面,可以继承给其他页面:base.html

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

    2、博客主页:index.html

    {% extends 'blog/demo_base.html' %}
    
    {% block title %}
        首页
    {% endblock %}
    
    {% block bodyblock %}
        <tr>
            <td><a href="#">添加文章</a></td>
            <td><a href="#">文章列表</a></td>
        </tr>
    {% endblock %}
    

    3、增加博客页面:add.html

    {% 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'> <br> <br><br>
            内容 <textarea name="content" id="content"
                         placeholder="请输入内容" cols="30" rows="10"></textarea>
            <button type="submit">发布博客</button>
        </form>
    {% endblock %}
    

    4、文章列表页面:list.html

    {% 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>
            <tr>
                <th><a href="">文章1</a></th>
                <th><a href="">编辑</a> | <a href="">删除 </a></th>
            </tr>
            <tr>
                <th><a href="">文章2</a></th>
                <th><a href="">编辑</a> | <a href="">删除 </a></th>
            </tr>
        </tbody>
        </table>
    
    
    {% endblock %}
    

    5、文章详情页面:detail.html

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

    6、文章修改页面:update.html

    {% 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'> <br> <br><br>
            内容 <textarea name="content" id="content"
                         placeholder="请输入内容" cols="30" rows="10"></textarea>
            <button type="submit">发布博客</button>
        </form>
    {% endblock %}
    

    准备工作完成,在pycharm 中建立blog的app。在主项目文件中的settings中进行注册,并设置主路由。

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'blog',   #注册
    ]
    
    from django.contrib import admin
    from django.urls import path,include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('blog/',include('blog.urls')),
    ]
    

    第一步:视图函数

    from django.shortcuts import render,redirect,reverse
    from django.http import HttpResponse
    from .models import BlogModel
    # Create your views here.
    def blog_index(request):
        return render(request,'blog/blog_index.html')
    
    def blog_add(request):
        if request.method == 'POST':
            title = request.POST.get('title')
            content = request.POST.get('content')
            print(title,content)
            blog = BlogModel(title=title,zcontent=content)
            blog.save()
        return render(request,'blog/blog_add.html')
    
    def blog_list(request):
        blog_list = BlogModel.objects.all()
        print(blog_list)
        return render(request,'blog/blog_list.html',context={'blog':blog_list})
    
    def blog_detail(request,blog_id):
        blog = BlogModel.objects.get(id=blog_id)
        return render(request,'blog/blog_detail.html',context={'blog':blog})
    
    def blog_delete(request,blog_id):
        blog = BlogModel.objects.filter(id=blog_id)
        print(blog)
        if blog:
            blog.delete()
            return redirect(reverse('blog_list'))
        else:
            return HttpResponse("这篇博客是不存在的")
    
    def blog_update(request,blog_id):
        blog = BlogModel.objects.filter(id=blog_id)
        print(blog)
        if request.method == 'GET':
            return render(request,'blog/blog_update.html',context={'blog':blog})
        elif request.method == 'POST':
            bl = blog[0]
            bl.title = request.POST.get('title')
            bl.content = request.POST.get('content')
            bl.save()
            # return render(request,'blog/blog_list.html')
            return redirect(reverse('blog_list'))
    

    分路由设置:

    from django.urls import path,include
    from . import views
    urlpatterns = [
        path('add/',views.blog_add,name='blog_add'),
        path('delete/<blog_id>/',views.blog_delete,name='blog_delete'),
        path('list/',views.blog_list,name='blog_list'),
        path('detail/<blog_id>/',views.blog_detail,name='blog_detail'),
        path('index/',views.blog_index,name='blog_index'),
        path('update/<blog_id>/',views.blog_update,name='blog_update'),
    ]
    

    此时,由于视图函数传参数的缘故,页面需要接受参数,修改如下:

    修改博客主页:index.html

    {% 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 %}
    

    修改文章列表页面:list.html

    {% 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 bg in blog %}
            <tr>
                <th><a href="{% url 'blog_detail' bg.id%}">{{ bg.title}}</a></th>
                <th><a href="{% url 'blog_updete' bg.id%}">编辑</a> | <a href="{% url 'blog_delete' bg.id%}">删除 </a></th>
            </tr>
        {% endfor %}
        </tbody>
        </table>
    
    
    {% endblock %}
    

    修改文章详情页面:detail.html

    {% extends 'blog/demo_base.html' %}
    {% block title %}
        文章详情
    {% endblock %}
    {% block bodyblock %}
        <h1>{{blog.title}}</h1>
        {{blog.content}}
    {% endblock %}
    

    修改文章修改页面:update.html

    {% 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>
            内容 <textarea name="content" id="content"
                         placeholder="请输入内容" cols="30" rows="10">{{blog.content}}</textarea>
            <button type="submit">发布博客</button>
        </form>
    {% endblock %}
    

    这样,一个博客的小案例就完成了,启动服务,就可以进行博客的增删改查了。

    相关文章

      网友评论

          本文标题:Django博客小案列

          本文链接:https://www.haomeiwen.com/subject/blashctx.html