django开发简易博客(四)
上一节,我们讲述了怎么使用静态文件,并使用bootstrap对页面进行了美化,这一节我们将增强我们blog的功能,添加发表博客,删除博客的功能。
一.表单的使用
要实现添加blog的功能,就得使用表单。
Django带有一个form库,称为django.forms,这个库可以处理我们本章所提到的包括HTML表单显示以及验证。当然我们现在有两个选择
- 自己写,完成表单显示以及验证
- 使用django提供的From
首先修改blog目录下urls.py,添加
url(r'^blog/add/$','blog_add',name='addblog'),
在blog目录下新建forms.py文件
from django import froms
  class BlogForm(forms.Form):
	    caption = forms.CharField(label = 'title' , max_length = 100)
	    content = forms.CharField(widget = forms.Textarea)
	    tag_name = forms.CharField()
新建blog_add.html
{% extends "blog_base.html" %}
{% block title %} 发表博客 {% endblock %}   
{% block article %} 
<form action="" method="post">
    {% csrf_token %}
    <div>
        <label for="id_caption">Title: </label>
        {% if form.caption.errors %}
        <div class="alert alert-error">
            {{ form.caption.errors }}
        </div>
        {% endif %}
        {{ form.caption }}
    </div>
    <div >
        <label for="id_content">Content: </label>
        {% if form.content.errors %}
        <div class="alert alert-error">
            {{ form.content.errors }}
        </div>
        {% endif %}
        {{ form.content }}
    </div>
   <div>
        <label for="id_tag">tags</label>
        {% if tag.tag_name.errors %}
        <div class="alert alert-error">
            {{ tag.tag_name.errors }}
        </div>
        {% endif %}
        {{ tag.tag_name }}
 </div>
    <div>
        <input class="btn btn-primary" type="submit" value="save and add">
    </div>
</form>
{% endblock %}
在views.py中添加红色部分
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from blog.models import Blog ,Tag ,Author
from django.http import Http404
from blog.forms import BlogForm ,TagForm def blog_list(request):
blogs = Blog.objects.all()
tags =Tag.objects.all()
return render_to_response("blog_list.html", {"blogs": blogs,"tags":tags}) def blog_show(request, id=''):
try:
blog = Blog.objects.get(id=id)
except Blog.DoesNotExist:
raise Http404
return render_to_response("blog_show.html", {"blog": blog}) def blog_filter(request,id=''):
tags = Tag.objects.all()
tag = Tag.objects.get(id=id)
blogs = tag.blog_set.all()
return render_to_response("blog_filter.html",{"blogs":blogs,"tag":tag,"tags":tags}) def blog_add(request):
if request.method == 'POST':
form = BlogForm(request.POST)
tag = TagForm(request.POST)
if form.is_valid() and tag.is_valid():
cd = form.cleaned_data
cdtag = tag.cleaned_data
tagname = cdtag['tag_name']
for taglist in tagname.split():
Tag.objects.get_or_create(tag_name=taglist.strip())
title = cd['caption']
author = Author.objects.get(id=1)
content = cd['content']
blog = Blog(caption=title, author=author, content=content)
blog.save()
for taglist in tagname.split():
blog.tags.add(Tag.objects.get(tag_name=taglist.strip()))
blog.save()
id = Blog.objects.order_by('-publish_time')[0].id
return HttpResponseRedirect('/web/blog/%s' % id)
else:
form = BlogForm()
tag = TagForm(initial={'tag_name': 'notags'})
return render_to_response('blog_add.html',
{'form': form, 'tag': tag}, context_instance=RequestContext(request))
使用 form的is_valid()方法,验证它的数据是否合法,如果一个Form实体的数据是合法的,它就会有一个可用的cleaned_data属性。 这是一个包含干净的提交数据的字典。
这里我们默认作者是id=1,当然你也可以自己修改或者根据登录的session读取
博客提交后 使用HttpResponseRedirect 跳转到最新发表的博客页面
因为我们使用的是post ,所以必须在表单后面添加 {% csrf_token %},然后在视图中使用 context_instance=RequestContext(request)。
重新编辑blog_list.html,在其中添加:
{% extends "blog_base.html" %}
{% block title %} 博文列表 {% endblock %}
{% block article %}
<article class='content'>
    {% for blog in blogs %}
        <h4><a href="{% url 'detailblog' blog.id %}">{{blog.caption}}</a></h4>
        <p class="muted">
            {% for tag in blog.tags.all %}
                <span  class="glyphicon glyphicon-tag"></span><small>{{tag}}</small>
            {% endfor %}
        </p>
        <div class="row">
            <div class="col-md-3">
               <span  class="glyphicon glyphicon-time"></span><small> {{ blog.publish_time }}</small>
            </div>
            <div class="col-md-2 col-md-offset-7">
            </div>
        </div>
        <hr>
    {% endfor %}
</article>
{% endblock %}
{% block aside %}
  <!--此处添加发表新博客的按钮--!>
    <a href="{% url 'addblog' %}" class="btn"> <span  class="glyphicon glyphicon-plus">发表新的博客</a>
    {% block tags %}
         <div class="well">
            {% for tag in tags %}
                <span class="label"><a href="{% url 'filterblog' tag.id %}">{{tag}}</a></span>
            {% endfor %}
    </div>
    {% endblock %}
{% endblock %}
二.编辑博客
在blog目录下的urls.py中添加
url(r'^blog/update/(?P<id>\w+)/$', 'blog_update',name='updateblog'),
因为更新和编辑的表单相同,所以使用跟blog_add.html作为模板
在views.py中添加如下内容
def blog_update(request, id=""):
id = id
if request.method == 'POST':
form = BlogForm(request.POST)
tag = TagForm(request.POST)
if form.is_valid() and tag.is_valid():
cd = form.cleaned_data
cdtag = tag.cleaned_data
tagname = cdtag['tag_name']
tagnamelist = tagname.split()
for taglist in tagnamelist:
Tag.objects.get_or_create(tag_name=taglist.strip())
title = cd['caption']
content = cd['content']
blog = Blog.objects.get(id=id)
if blog:
blog.caption = title
blog.content = content
blog.save()
for taglist in tagnamelist:
blog.tags.add(Tag.objects.get(tag_name=taglist.strip()))
blog.save()
tags = blog.tags.all()
for tagname in tags:
tagname = unicode(str(tagname), "utf-8")
if tagname not in tagnamelist:
notag = blog.tags.get(tag_name=tagname)
blog.tags.remove(notag)
else:
blog = Blog(caption=blog.caption, content=blog.content)
blog.save()
return HttpResponseRedirect('/sblog/blog/%s' % id)
else:
try:
blog = Blog.objects.get(id=id)
except Exception:
raise Http404
form = BlogForm(initial={'caption': blog.caption, 'content': blog.content}, auto_id=False)
tags = blog.tags.all()
if tags:
taginit = ''
for x in tags:
taginit += str(x) + ' '
tag = TagForm(initial={'tag_name': taginit})
else:
tag = TagForm()
return render_to_response('blog_add.html',
{'blog': blog, 'form': form, 'id': id, 'tag': tag},
context_instance=RequestContext(request))
在blog_list.html中添加(红色部分)
<div class="row">
<div class="col-md-3">
<span class="glyphicon glyphicon-time"></span><small> {{ blog.publish_time }}</small>
</div>
<div class="col-md-2 col-md-offset-7">
<a href="{% url 'updateblog' blog.id %}" title="edit">
<span class="glyphicon glyphicon-edit"></span>
</a>
</div>
</div>
在blog_show.html中添加(红色部分)
<div class="col-md-2 col-md-offset-7">
<a href="{% url 'updateblog' blog.id %}" title="edit">
<span class="glyphicon glyphicon-edit"></span>
</a>
</div>
同样,在blog_filter.html中添加以上内容。
三.删除文章
修改blog目录下的urls.py,添加
url(r'^blog/del/(?P<id>\w+)/$', 'blog_del', name='delblog'),
修改views.py添加
def blog_del(request, id=""):
try:
blog = Blog.objects.get(id=id)
except Exception:
raise Http404
if blog:
blog.delete()
return HttpResponseRedirect("/web/bloglist/")
blogs = Blog.objects.all()
return render_to_response("blog_list.html", {"blogs": blogs})
在blog_list.list 、blog_show.html、blog_filter.html中添加红色加粗部分代码
<div class="col-md-2 col-md-offset-7">
<a href="{% url 'updateblog' blog.id %}" title="edit">
<span class="glyphicon glyphicon-edit"></span>
</a>
<a href="{% url 'delblog' blog.id %}" title="delete">
<span class="glyphicon glyphicon-trash"></span>
</a>
</div>
这样一个具有增删改的博客站点就完成了,但是我们可以看到使用django自定义的表单对象,界面还是非常丑的,需要自己进行重新进行美化。
django开发简易博客(四)的更多相关文章
- django开发简易博客(一)
		这几篇博客是根据GoodSpeed的博客该写的,看了他的博客收获很大,但是他的博客从第三篇开始,条理很不清晰,加之又是几年之前写的,编写环境发生很大改变,所以对他的博客进行了一个整理,加入了一些自己的 ... 
- django 开发简易博客(二)
		这一节我们来了解模板和视图.URL的使用. 一.使用模板 在blog目录中新建templates文件夹,在templates文件夹下新建base.html文件.目录结构如下 templates/ ba ... 
- django开发简易博客(五)
		这一节将讲述如何添加comments库与ajax的支持. 一.添加comments库 comments库是是django框架内置的一个评论库,可以快速的搭建岀一个评论系统,不过再自定义方面有些麻烦,不 ... 
- django开发简易博客(三)
		一.静态文件的使用 首先,新建static目录,目录下分别建立css.js.img三个子目录 修改settings.py文件 STATICFILES_DIRS = ( 'F:/web/static', ... 
- Django搭建简易博客
		Django简易博客,主要实现了以下功能 连接数据库 创建超级用户与后台管理 利用django-admin-bootstrap美化界面 template,view与动态URL 多说评论功能 Markd ... 
- Django开发个人博客入门学习经验贴
		[写在前面] 入门学习搭建个人博客系统首先还是参考大佬们的经验,记得刚入手Django的时候,一篇博客大佬说过一句话,做技术的不要一开始就扎头于细节中,先把握整体框架,了解这个对象之后再去了解细节,进 ... 
- Django搭建简易博客教程(四)-Models
		原文链接: http://www.jianshu.com/p/dbc4193b4f95 Django Model 每一个Django Model都继承自django.db.models.Model 在 ... 
- Django 搭建简易博客
		新增一个 APP 博客算是一个功能集,因此我们应将其体现为一个模块.这表现在 Django 应用里则是为其创建一个 APP Package.现在让 manage.py 中的 startapp 闪亮登场 ... 
- 实战Django:简易博客Part1
		舍得学习新技能的时候,通常不喜欢傻读书--捧着一本阐述该项技能的书籍,然后傻看,一路看下来,脑子里塞满了新的概念.知识点,头是越来越大,但技能却几乎没掌握半分. 多年来,舍得养成了用做实例来学习新技能 ... 
随机推荐
- Dijkstra算法模拟讲解
			dijkstra算法,是一个求单源最短路径算法 其算法的特点为: 层层逼进,有点类似宽度搜索的感觉 其需要的数据结构为: int map[N][N] 所有点之间的权表 ... 
- Javascript 风格向导
			序 大部分针对Javascript最合理的方法归纳. 类型 • 原始类型:我们可以直接使用值. ο string ο number ο boolean ο null ο undefined ... 
- POJ 1226 Substrings(后缀数组+二分答案)
			[题目链接] http://poj.org/problem?id=1226 [题目大意] 求在每个给出字符串中出现的最长子串的长度,字符串在出现的时候可以是倒置的. [题解] 我们将每个字符串倒置,用 ... 
- Windows Latex 中日文字体设置例
			中文字体例: \documentclass[CJK]{article} \usepackage{CJKutf8} \newcommand{\songti}{\CJKfamily{song}} % 宋体 ... 
- jQuery Lint: enables you to automatically inject jQuery Lint into the page as it is loaded (great for ad-hoc code validation)
			FireQuery is a Firebug extension for jQuery development jQuery Lint: enables you to automatically in ... 
- Reorder the Books(规律)
			Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ... 
- QVector 和vector的比较
			QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快, ... 
- EF 如何code first
			首先配置连接数据.sql server <connectionStrings> <add name="Model1" connectionString=" ... 
- nginx 使用安装问题及解决方案
			如何安装清参考:http://www.runoob.com/linux/nginx-install-setup.html 可以先不用配置,等理解后在配置. 在启动nginx时,出现提示: nginx: ... 
- xml 充当简易数据库
			后台: 写入节点 public static void Update(string path, string node, string attribute, string value) { try { ... 
