您的位置:首页 > 编程语言 > Go语言

django 简易博客开发 3 静态文件、from 应用与自定义

2012-10-01 13:04 591 查看
首先还是贴一下源代码地址 https://github.com/goodspeedcheng/sblog

上一篇博客我们介绍了 django 如何在views中使用templates以及一些常用的数据操作,这篇博客将介绍静态文件的使用、from 应用与自定义

1、静态文件的使用

鉴于我们上次所看到的界面惨不忍睹,为了不影响心情,先介绍一下如何使用静态文件美化界面

首先新建static目录,目录下新建css/js/img三个目录

修改seeting.py文件

STATICFILES_DIRS = (
'/home/gs/blog/static',   #替换成自己的static 目录
)


修改blog目录下 urls.py 添加以下内容

urlpatterns += patterns((''),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/home/gs/blog/static'}
),
)


当然这只是本机测试环境下的使用方法,生产环境这样部署是不可以的。

2、使用bootstrap美化界面

为了方便,我们使用bootstrap进行界面优化,如果你想自己写界面,这一步完全可以跳过去

bootstrap 下载地址 http://twitter.github.com/bootstrap/

中文文档:http://wrongwaycn.github.com/bootstrap/docs/index.html

bootstrap使用非常方便下载解压后 将其中的css/js/img 目录中文件分别放入static目录下相应文件夹就好了,然后修改base.html,在head标签添加

<link rel="stylesheet" href="/static/css/bootstrap.css">
<link rel="stylesheet" href="/static/css/reset.css">
<link rel="stylesheet" href="/static/css/style.css">
<link rel="stylesheet" href="/static/css/code.css">
<script src="/static/js/modernizr.js"></script>
<script src="/static/js/jquery.js"></script>



因为bootstrap需要jquery支持,所以必须要引入jquery ,因为我们使用的html5,使用modernizr会自动检测不兼容的浏览器利用js添加相应的功能。


在body添加

<script src="/static/js/bootstrap.min.js"></script>


现在base.html是这个样子的

View Code

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))


其中

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)


作用是将tag与blog关联,并且去掉原有关联修改后不关联的tag 例如blog1 原有tag为1,2 ,3 修改后为2, 3

这里的作用是去除blog1与tag 1的关联

5、删除文章

修改urls.py 添加

url(r'^blog/(?P<id>\w+)/del/$', '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("/sblog/bloglist/")
blogs = Blog.objects.all()
return render_to_response("blog_list.html", {"blogs": blogs})


完成后发现我们并没有相关的update delete 链接

将第二步中的注释去掉吧 现在刷新 blog show 页面是否看到了呢

最后源代码可以在 https://github.com/goodspeedcheng/sblog 可以看一下 希望大家把错误的地方提出纠正一下。

谢谢

以上 关于from的内容能在 django book 2 第七章找到

扩展阅读: https://docs.djangoproject.com/en/1.4/

http://twitter.github.com/bootstrap/index.html

推荐 Django 最佳实践 - 中文版 https://github.com/brantyoung/zh-django-best-practices/blob/master/readme.rst/

ps: 大四学生求实习 邮箱: cacique1103#gmail.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: