python 3.5
一、后台管理系统:xadmin
将下载的xadmin文件夹复制到项目根目录
pip install httplib2 django-formtools django-crispy-forms
pip install six
INSTALLED_APPS = [ ...'xadmin', 'crispy_forms' ]
import xadmin urlpatterns = [ url(r'^admin/', xadmin.site.urls), ]
一定要:否则会出现programminerror
makemigrations
migrate
二、注册验证码:django-simple-captcha
源码地址:https://github.com/mbi/django-simple-captcha
参考文档:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation
安装:Pillow
pip install django-simple-captcha #或在项目解释器中安装;版本:0.5.6
添加captcha 到
INSTALLED_APPS
url(r'^captcha/', include('captcha.urls')), #url中配置
makemigrations、migrate
数据库中生成表:captcha_captchastore
forms.py中:
class RegisterForm(forms.Form): email = forms.EmailField(required=True) password = forms.CharField(required=True, min_length=6) captcha = CaptchaField(error_messages={'invalid':'验证码错误'}) #生产验证码字段,并自定义错误信息,键是invilid
views.py中:
class RegisterView(View): def get(self,request): register_form = RegisterForm() #生产实例 return render(request,'register.html',{'register_form':register_form}) #传入模板
register.html中:
<div class="form-group marb8 captcha1 "> <label>验 证 码</label> {{ register_form.captcha }} #模板中应用 </div>
三、分页功能:django-pure-pagination
参考:https://github.com/jamespacileo/django-pure-pagination
a、通过pip或在django中安装
b、添加到installed apps
'pure_pagination',
c、settings中不需要自己设置
d、views中:
from django.shortcuts import render from django.views import View from .models import * from pure_pagination import * class OrgView(View): def get(self,request): all_orgs = CourseOrg.objects.all() #从数据库中获取所有机构对象列表 all_cities = CityDict.objects.all() #城市对象列表 all_nums = all_orgs.count() #机构总数 #对课程机构进行分页 try: page = request.GET.get('page', 1) #获取当前页码 p = Paginator(all_orgs, 2, request=request) #参数1是所有的数据列表,2是每页显示信息数 orgs = p.page(page) #获取当前页信息 except PageNotAnInteger: #如果页码不是整型,就取首页 orgs = p.page(1) except EmptyPage: #如果页码超出范围,会报该错误,所以捕捉并处理 orgs = p.page(1) context = { 'all_orgs':orgs, 'all_cities':all_cities, 'all_nums':all_nums } return render(request,'org-list.html',context=context)
e、模版中:
{% for course_org in all_orgs.object_list %} #显示逻辑
...
<h1>{{ course_org.name }}</h1>
...
{% endfor %} <ul> #分页逻辑 {% if all_orgs.has_previous %} #如果当前页有上一页就显示上一页页码并能跳转 <li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}">上一页</a></li> {% endif %} {% for page in all_orgs.pages %} #迭代所有页码 {% if page %} {% ifequal page all_orgs.number %} #页码与当前显示页页码相同,就将当前页码显示为特殊颜色如灰色,见下图 <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li> {% else %} <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li> {% endifequal %} {% else %} <li class="none"><a href="">...</a></li> {% endif %} {% endfor %} {% if all_orgs.has_next %} #如果当前页有下一页就显示下一页页码并能跳转 <li class="long"><a href="?{{ all_orgs.next_page_number.querystring }}">下一页</a></li> {% endif %} </ul>
四、DjangoUeditor
源码:https://github.com/twz915/DjangoUeditor3/
1、拷贝里面的DjangoUeditor目录到extra_apps
2、添加到设置中app:
'DjangoUeditor'
3、url
#富文本相关url url(r'^ueditor/',include('DjangoUeditor.urls' )),
4、模型中使用该字段:
from DjangoUeditor.models import UEditorField #导入
detail = UEditorField(verbose_name='课程详情',width=600, height=300, imagePath="courses/ueditor/", filePath="courses/ueditor/",default='')
5、制作xadmin插件
a、在xadmin下的plugins插件管理目录中新建插件ueditor.py并添加到初始化文件中的PLUGINS元组中。
b、写插件
c、在应用的adminx文件中添加:
style_fields = {'detail':'ueditor'} #指明哪个字段使用富文本编辑器
6、最后在html文件中关闭对应字段的自动转义
{% autoescape off %}
{{ course.detail }}
{% endautoescape %}