0

我从 forms.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 %}

forms.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 %}

视图.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 加载函数加载此表单时缺少 csrf 令牌。

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

4

1 回答 1

0

阅读 Django 文档中的 ajax 部分

于 2013-08-18T06:25:22.793 回答