jquery.load函数无法加载csrf标记fron django模板,缺少csrf标记

时间:2013-08-18 04:13:47

标签: jquery django

我从form.html模板加载表单的index.html模板:

{% extends 'base.html' %}

{% load static %}

{% block title %} jQuery Load Function {% endblock %}

{% block extrahead %}

<script type="text/javascript" src=" {% static "learn_static_files/scripts/jquery.min.js" %} "></script>


<script>
    $('document').ready(function(){
        $("#register").on("click", function(){
              $("#register_signup").load("{% url 'learn.views.get_latest_news' %} #register");
        });
        $("#signIn").on("click", function(){
              $("#register_signup").load("{% url 'learn.views.get_latest_news' %} #signin");
        });
    });
</script>
{% endblock %}

{% block content%}
<div id="register">Register</div>
<div id="signIn">Sign In</div>
<div id="register_signup"></div>        
{% endblock %}

form.html从哪里加载表单,其中包含csrf标记:

{% extends 'base.html' %}
{% block content %}
<div id="register">
<form action="/learn/register/" method="post">
    {% csrf_token %}
    <p>
        <label for="name">name</label>
        <input type="text" placeholder="name" name="name">
    </p>
    <p>
        <label for="username">Username</label>
        <input type="text" placeholder="username" name="username">
    </p>
    <p>
        <label for="password">Password</label>
        <input type="password" name="password">
    </p>
    <p>
        <label for="varify_password">Varify Password</label>
        <input type="password" name="varify_password">
    </p>
    <p>
        <input type="submit" name="register" value="register">
    </p>
</form>
</div>

<div id="signin">
<form action="/learn/sign_in/" method="post">
    {% csrf_token %}
    <p>
        <label for="username">Username</label>
        <input type="text" placeholder="username" name="username">
    </p>
    <p>
        <label for="password">Password</label>
        <input type="password" name="password">
    </p>
    <p>
        <input type="submit" name="signin" value="sign in">
    </p>
</form>
</div>

{% endblock %}

views.py:

from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.template import RequestContext

from learn.models import Registration
from learn.forms import RegistrationForm
from django.contrib.auth.models import User

def index(request):
   return render_to_response('index.html')

def get_latest_news(request):
    return render_to_response('forms.html')

def register(request):
    if request.method == 'POST':
    form = RegistrationForm(request.POST)
    if form.is_valid():
        user = User(username=form.cleaned_data['username'],password=form.cleaned_data['password'])
        user.save()
        registered_user = Registration(user=user, name=form.cleaned_data['name'])
        registered_user.save()
        return HttpResonse('form is successfully submitted')
    else:
        return render_to_response('forms.html',{'form':form},context_instance=RequestContext(request))
else:
    form = RegistrationForm()
    return render_to_response('forms.html',{'form':form},context_instance=RequestContext(request))

我在点击事件上在索引页面的div #register_signup上调用jquery.load函数。     如果使用寄存器视图直接从url调用寄存器表单,那么它显示没有问题,并且csrf_token也可用,但是当通过jquery load函数加载此表单时,缺少csrf标记。

请建议我在哪里做错了。我想在index.html中加载注册和登录表单。

1 个答案:

答案 0 :(得分:0)