mooc在线教育网开发流程总结(四)template

 

1、模本文件的基本修改

资源文件的写法:

加上{% load staticfiles %}
,修改资源文件为{% static 'css/rs.css' %}

src="{{ MEDIA_URL }}{{ banner_course.image }}" 
href="#model{{ good_class.id }}"
{% ifequal good.goods_class good_class %}

设置里面模板里面要引入下面才能使用MEDIA_URL:

'django.template.context_processors.media' 
视图里面合并多个queryset的方法:
        popularitys = Goods.objects.none()
        for goods_class in goods_classes:
            popularity = Goods.objects.filter(goods_class=goods_class).order_by("popularity")[:3]
            popularitys = chain(popularitys, popularity)

2、模板继承,写了个dummybase.html方便拷贝修改:

{% extends "base.html" %}
{% load staticfiles %}
{% block title %}天天生鲜{% endblock %}

{% block custom_js %}
{% endblock %}

{% block custom_header %}
{% endblock %}

{% block custom_search %}
{% endblock %}

{% block custom_content %}
{% endblock %}
3、视图里面访问资源文件:
from django.views.static import serve
    url(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),
    url(r'^static/(?P<path>.*)$', serve, {'document_root': STATIC_ROOT}),
4、修改forms:
from django import forms
class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['username','password','email']        
form = UserProfileForm(request.POST)

5、模板里面使用forms:

修改form的action为post

<form method="post" action="{% url 'register' %}">

<input type="text" name="user_name" id="user_name" value="{{ form.username.value }}" {% if form.errors.username %} errorput {% endif %} 

#注意name是数据库字段名

{% if not form.errors.items %} {{ msg }} {% endif %}

1.为什么errors.username

form.clean_data字典在html里面用.号访问,errors也是字典

2.为什么模板没有循环

可以模板断点查看,原因是chain是生成器只能作用一次

modelformset_factory(Author, fields=('name', 'title'))#有一定html格式的form
modelform_factory(Author, fields=('name', 'title'))#model工厂,快速创建非定制化的form
Poll.objects.get(
    Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)),
    question__startswith='Who')

user.models.DoesNotExist: UserProfile matching query does not exist.

如果用get查询查询不到值会抛异常。

如果删除字段名了,可能导致迁移目录的initail文件异常,删除重新生成即可。

import sys
sys.path.append('D:/python/prj/tiantian/apps')
from user.models import UserProfile
UserProfile.objects.filter(username='root')
#本地console调试代码,如果项目代码修改,可能需要重新运行console才能生效

new_user = form.save()
new_user.password =make_password(form.data['password'])
new_user.save()
#form数据保存在数据库

6、如果需要使用登录的用户{% if request.user.is_authenticated %},使用request.user的方式使用

7、模板里面使用ajax,使用的变量和正常的一样

$('#js-pl-submit').on('click', function () {
    var comments = $("#js-pl-textarea").val()
    if (comments == "") {
        alert("评论不能为空")
        return
    }
    $.ajax({
        cache: false,
        type: "POST",
        url: "{% url 'courses:add_comment' %}",
        data: {course_id: {{ course.id }}, comments: comments},
        async: true,
        beforeSend: function (xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
        },#post也是要加上csrftoken
        success: function (data) {
            if (data.status == 'fail') {
              if (data.msg == '用户未登录') {
                  window.location.href = "{% url 'login' %}";
              } else {
                  alert(data.msg)
              }
            } else if (data.status == 'success') {
                alert('评论成功!')
                window.location.reload();//刷新当前页面.
            }
        },
        error: function(error) {
          alert('ajax 失败!')
        }
    });
});

js里面不能打断点调试,只能通过浏览器找到问题的地方,可以看到下图里面ajax里面的参数都显示出来了。


打上两个断点后,右上角的调试工具就能用了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值