django--CBV、csrf

CBV模式

url(r'^cbv/', views.CBV.as_view()),
url
from django.views import View
class CBV(View):
    def dispatch(self, request, *args, **kwargs):#重构dispatch
        print('做些自己想做的事')
        result = super(CBV,self).dispatch( request, *args, **kwargs)
        #result:继承源码的dispatch
        return result

    def get(self,reqtest):
        return render(reqtest,'cbv.html')

    def post(self,requset):
        return HttpResponse("POST")
Views
<form method="post" action="cbv.html">
    {% csrf_token %}
    <input type="submit">
</form>
html

 

八种提交方法:'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'
  form表单只能用get和post,其他用Ajax

模板语言设置序号:for循环中:{{ forloop.counter }}

    form提交
            <form method="post" action="/login/">
                {% csrf_token %}
            </form>
    Ajax提交
            <script>
                $(function () {
                    $.ajaxSetup({
                        beforeSend:function (xhr,settings) {
                            xhr.setRequestHeader('X-CSRFtoken',$.cookie('csrftoken'));
                        }
                    });
            <script>
csrf_token

 

posted @ 2018-03-13 11:31  web123  阅读(38)  评论(0)    收藏  举报