Django1.9.8 + Xadmin + Windows 快速搭建网站二
1.很多页面具有共同的头部底部
django 模板继承机制 参数向上传递 因此可以将用户是否登录的一些判断逻辑在base.html页面中使用
base.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <title>{% block title %}首页{% endblock %}</title> <link rel="stylesheet" type="text/css" href="/static/css/reset.css"> <link rel="stylesheet" type="text/css" href="/static/css/animate.css"> <link rel="stylesheet" type="text/css" href="/static/css/style.css"> {% block custom_css %}{% endblock %} <script src="/static/js/jquery.min.js" type="text/javascript"></script> <script src="/static/js/jquery-migrate-1.2.1.min.js" type="text/javascript"></script> {% block custom_js %}{% endblock %} </head> <body> <section class="headerwrap "> 此处是页面头部 </section> {% block content %} 此处是页面的内容 {% endblock %} <footer> 此处是页面底部 <div class="footer"> </div> </footer> <section> <ul class="sidebar"> <li class="qq"> <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=2023525077&site=qq&menu=yes"></a> </li> <li class="totop"></li> </ul> </section> <script src="/static/js/selectUi.js" type='text/javascript'></script> <script src="/static/js/deco-common.js" type='text/javascript'></script> <script type="text/javascript" src="/static/js/plugins/laydate/laydate.js"></script> <script src="/static/js/plugins/layer/layer.js"></script> <script src="/static/js/plugins/queryCity/js/public.js" type="text/javascript"></script> <script src="/static/js/unslider.js" type="text/javascript"></script> <script src="/static/js/plugins/jquery.scrollLoading.js" type="text/javascript"></script> <script src="/static/js/deco-common.js" type="text/javascript"></script> </body> </html>
子页面进行继承
{% extends "base.html" %} {% block title %} 子页面修改标题 {% endblock %} {% block content %} <section> 子页面添加新的要填充的内容 </section> {% endblock %}
2.配置上传文件的文件夹位置
1 MEDIA_URL = '/media/' 2 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
3.将配置的media路径注册到html当中setting文件中的context_processors中添加如下配置
1 TEMPLATES = [ 2 { 3 'OPTIONS': { 4 'context_processors': [ 5 'django.core.context_processors.media', 6 ], 7 }, 8 }, 9 ]
4.url配置(配置上传文件的访问处理)
from django.views.static import serve from djangoPro.settings import MEDIA_ROOT url(r'^media/(?P<path>.*)$', serve, {"document_root":MEDIA_ROOT})
5.html代码中直接使用
#org.image 存放的是image的路径 {{ MEDIA_URL }}{{ org.image }} #最终得到的路径是 #/media/org/2017/10/image.png
6.分页
1.添加设置
pip install django-pure-pagination #setting中添加设置 INSTALLED_APPS = ( ... 'pure_pagination', ) PAGINATION_SETTINGS = { 'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True, }
2.view中添加使用分页代码
1 allOrgs = courseOrg.objects.all() 2 try: 3 page = request.GET.get('page', 1) 4 except PageNotAnInteger: 5 page = 1 6 7 p = Paginator(allOrgs,5, request=request) 8 9 orgs = p.page(page) 10 11 return render(request, 'org-list.html', { 12 "allOrgs":orgs, 13 14 })
3.template页面中使用分页
1 {% if allOrgs.has_previous %}<!--上一页逻辑 如果有前一页 则显示上一页--> 2 <li class="long"> 3 <a href="?{{ allOrgs.previous_page_number.querystring }}" class="prev">上一页</a> 4 </li> 5 {% endif %} 6 7 {% for page in allOrgs.pages %}<!--当前页逻辑--> 8 {% if page %} 9 {% ifequal page allOrgs.number %} 10 <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li> 11 {% else %} 12 <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li> 13 {% endifequal %} 14 {% else %} 15 <li class="none"><a href="">...</a></li> 16 {% endif %} 17 {% endfor %} 18 19 {% if allOrgs.has_next %}<!--下一页逻辑--> 20 <li class="long"><a href="?{{ allOrgs.next_page_number.querystring }}">下一页</a></li> 21 {% endif %}
7.django -ModelForm
原始写法
1 class loginForm(forms.Form): 2 #此处字段名称必须与html中的字段一样才可以进行校验 3 username = forms.CharField(required=True) 4 password = forms.CharField(required=True, min_length=5)
使用ModelForm(forms.py)
1 from django import forms 2 3 from operation.models import userAsk 4 5 class userAskForm(forms.ModelForm): 6 #可以添加自定义字段 7 class Meta: 8 model = userAsk 9 #使用定义好的model里面的字段 10 fields = ['name','mobile','askCourseName']
1 #clean开头 自定义验证手机号码 ModelForm 2 def clean_mobile(self): 3 # 手机号验证 4 mobile = self.cleaned_data['mobile'] 5 p = re.compile('^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}') 6 if p.match(mobile): 7 return mobile 8 raise forms.ValidationError('手机号码格式不对', code='mobile_invalid')
异步请求(views.py)
1 class addUserAskView(View): 2 def post(self, request): 3 userask_form = userAskForm(request.POST) 4 if userask_form.is_valid(): 5 user_ask = userask_form.save(commit=True) 6 return HttpResponse('{"status":"success"}', content_type='application/json') 7 else: 8 return HttpResponse('{"status":"fail", "msg":"添加出错"}', content_type='application/json')
使用ajax进行一步请求(html)
1 {% block custom_js %} 2 <script> 3 $(function () { 4 $('#jsStayBtn').on('click', function () { 5 $.ajax({ 6 cache: false, 7 type: "POST", 8 dataType: "json", 9 url: "{% url 'org:addAsk' %}", 10 //表单提交可以用 serialize 方法把 csrf token 一块序列化过来 11 data: $('#jsStayForm').serialize(), 12 async: true, 13 success: function (data) { 14 if (data.status == 'success') { 15 $('#jsStayForm')[0].reset(); 16 alert("提交成功") 17 } else if (data.status == 'fail') { 18 $('#jsCompanyTips').html(data.msg) 19 } 20 }, 21 error: function (error) { 22 console.log('error') 23 // console.log(error.responseText.msg) 24 } 25 }); 26 }); 27 }) 28 </script> 29 {% endblock %}