主要用到的是Django Ueditor的第三包
官方文档
https://github.com/zhangfisher/DjangoUeditor/blob/master/readme.md
1.安装
pip install DjangoUeditor
2.添加app
在INSTALL_APPS里面增加DjangoUeditor app,如下:
INSTALLED_APPS = (
#........
'DjangoUeditor',
)
3.配置urls
url(r'^ueditor/',include('DjangoUeditor.urls' )),
4.model.py
class BBS(models.Model):
title = models.CharField(verbose_name='标题', max_length=200)
summary = models.CharField(verbose_name='简介', max_length=200,blank=True,null=True)
content = UEditorField(verbose_name='内容', width=600, height=300, toolbars='mini', imagePath="upload/image/", settings={}, command=None, blank=True)
author = models.ForeignKey(User)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
category = models.ForeignKey('Category')
class Meta:
ordering=['-create_date']
db_table = 'bbs'
def __unicode__(self):
return self.title
5.form.py 使用modelForm创建表单
form django import forms
class PubForm(forms.ModelForm):
''' 使用modelForm 创建表单 '''
class Meta:
model = BBS
fields = ['title', 'summary', 'content','category']
6.view.py
def pub(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/bbs/login/')
else:
category_list = Category.objects.all()
if request.method == 'POST':
form = PubForm(request.POST.copy())
if form.is_valid():
bbs = form.save(commit=False)
bbs.author = request.user
bbs.save()
return HttpResponseRedirect('/bbs/')
else:
form = PubForm()
return render_to_response('pub.html',{'form':form, 'category_list':category_list},context_instance=RequestContext(request))
7.在模板中添加{{form.media}}
<head>
......
{{ form.media }} #这一句会将所需要的CSS和JS加进来。
......
</head>
注:运行collectstatic命令,将所依赖的css,js之类的文件复制到{{STATIC_ROOT}}文件夹里面。
网友评论