Django开发简单的blog

 

本项目基于学习阶段,只是涉及简单的crud与模板变量等操作.

1.创建blog app项目:

python manage.py startapp blog,并在项目主目录的settings.py中注册该app

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'book',
    'common',
    'info',
    'blog',
]

 

2.创建好app后,首先创建数据库模型models.py:

from django.db import models

# Create your models here.

class Blog(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=30)
    content = models.TextField(max_length=300)

并通过manage.py管理工具生成迁移文件,并映谢到数据库中.

3.在blog APP中创建templates模板文件目录并在里面创建blog目录

 

 主要页面有:主页,列表页,添加页,编辑页,详情页与模板基类页

base.html:

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

add.html:

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

{% block title %}
    添加文章
{% endblock %}

{% block body %}
    <h1>添加文章</h1><br>
    <form action="" method="post">
    {% csrf_token %}
    标题:<input type="text" name="title"><br>
    内容:<textarea placeholder="请输入内容" name="content" cols="30" rows="10"></textarea><br>
    <button type="submit">发布文章</button>
    </form>
{% endblock %}

index.html:

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

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

{% block body %}
       <tr>
        <td><a href={% url 'blog_add' %}>添加文章</a></td>
        <td><a href={% url 'blog_list' %}>文章列表</a></td>
         </tr>
{% endblock %}

list.html:

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

{% block title %}
    文章列表
{% endblock %}

{% block body %}
    <h1>文章列表</h1>
    <table width="400px">
        <thead>
            <tr>
                <th>标题</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for blog in blogs %}
                <tr>
                    <th><a href={% url 'blog_detail' blog.id %}>{{ blog.title }}</a></th>
                    <th><a href={% url 'blog_modify' blog.id %}>编辑</a>|<a href={% url 'blog_delete' blog.id %}>删除</a></th>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}

edit.html:

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

{% block title %}
修改文章
{% endblock %}

{% block body %}
<h1>修改文章</h1><br>
<form action="" method="post">
{% csrf_token %}
标题:<input type="text" name="title" value={{ blog.title }}><br>
内容:<textarea placeholder="请输入内容" name="content" cols="30" rows="10">{{ blog.content }}</textarea><br>
<button type="submit">修改</button>
</form>
{% endblock %}

detail.html:

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

{% block title %}
{{ blog.title }}
{% endblock %}

{% block body %}
<h1>文章详情</h1>
{{ blog.content }}
{% endblock %}

 

4.创建好模板文件后,创建相对应的urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^$', views.index,name='blog_index'),
url(r'^add/$', views.add,name='blog_add'),
url(r'^list/$', views.list,name='blog_list'),
url(r'^detail/(?P<blog_id>\d+)$', views.detail,name='blog_detail'),
url(r'^delete/(?P<blog_id>\d+)$', views.delete,name='blog_delete'),
url(r'^modify/(?P<blog_id>\d+)$', views.modify,name='blog_modify'),

]

5.创建相对应的视图文件views.py:

from django.shortcuts import render,HttpResponse,reverse,redirect
from .models import Blog
# Create your views here.

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

def add(request):
if request.method == 'GET':
return render(request,'blog/add.html')
elif request.method == 'POST':
#获取表单内容
title = request.POST.get('title')
content = request.POST.get('content')
#添加到数据库中
Blog.objects.create(title=title,content=content)
return HttpResponse('发布成功')
else:
return HttpResponse('这是不能处理的操作')


def list(request):
blogs = Blog.objects.all()
return render(request,'blog/list.html',{'blogs':blogs})

def detail(request,blog_id=0):
blog = Blog.objects.get(id=blog_id)
return render(request,'blog/detail.html',{'blog':blog})

def delete(request,blog_id=0):
blog = Blog.objects.get(id=blog_id)
if blog:
blog.delete()
return redirect(reverse('blog_list'))
else:
return HttpResponse('要删除的数据不存在')

def modify(request,blog_id=0):
blog = Blog.objects.get(id=blog_id)
if blog:
if request.method == 'GET':
return render(request, 'blog/edit.html', context={'blog': blog})
elif request.method == 'POST':
title = request.POST.get('title')
content = request.POST.get('content')
blog.title = title
blog.content = content
blog.save()
return HttpResponse('修改成功')
else:
return HttpResponse('该操作无法执行')
else:
return HttpResponse('数据不存在')

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

{% block title %}
添加文章
{% endblock %}

{% block body %}
<h1>添加文章</h1><br>
<form action="" method="post">
{% csrf_token %}
标题:<input type="text" name="title"><br>
内容:<textarea placeholder="请输入内容" name="content" cols="30" rows="10"></textarea><br>
<button type="submit">发布文章</button>
</form>
{% endblock %}

posted on 2018-06-04 18:29 remoting_python 阅读() 评论() 编辑 收藏

版权声明:本文为remoting-py原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/remoting-py/p/9134837.html