Django形式“正好采用1个参数(给定2个)"错误-可能与CSRF有关? [英] Django form "takes exactly 1 argument (2 given) " error - possibly related to CSRF?

查看:97
本文介绍了Django形式“正好采用1个参数(给定2个)"错误-可能与CSRF有关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django 1.3上尝试一种相当简单的形式,并试图了解CSRF的工作原理.

I am attempting a fairly simple form on Django 1.3, and am trying to understand how CSRF works.

我认为我已经按照在Django网站上详细介绍的三个步骤,但仍然无法使该表格生效.

I think I've followed the three steps detailed on the Django site, but am still unable to make the form work.

在加载URL时显示表单,但是在单击提交"按钮时,出现以下错误:

The form displays when the URL is loaded, however upon hitting the submit button, I get the following error:

在/txt/txt()处的

TypeError恰好采用 1个论点(给出2个)

TypeError at /txt/ txt() takes exactly 1 argument (2 given)

这是实际的代码:

views.py:

from django.http import HttpResponse
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext

def txt(request):
    if request.method == 'POST':
        msg="txt received"
    else:
        msg="nothing in POST"
    return render_to_response('base.html', locals(), context_instance=RequestContext(request))

HTML:

<body>

    <form action="txt/" method="POST">{% csrf_token %}
        From <input type="text" name="From"><br>
        To <input type="text" name="To"><br>
        Body <input type="text" name="Body"><br>
        <input type="submit" value="Search">
    </form>

    {{ msg }}

</body>

我知道我还没有完成form.py等,但是我只是想尝试一些基本功能.我认为这段代码在Django的早期版本中可以使用,并且不确定为什么这次不能使用.

I know I haven't done a forms.py etc. but I was just trying to get the basic functionality up and going. I think this code would have worked in previous versions of Django, and am unsure why its not working this time.

推荐答案

该错误似乎表明您的视图函数获取的参数多于设置要接受的参数.如上所示,该视图仅接受一个参数:请求.

The error looks like your view function is getting more arguments than it is setup to accept. As you have it shown above, the view only accepts a single argument: the request.

如果您为此视图的URL模式配置了捕获组,或者您正在通过

If your URL pattern for this view is configured with a capturing group, or you are adding extra arguments via the optional dictionary or the kwargs parameter to url(), then those extra arguments will be given to the view function and could cause the error you're seeing.

这篇关于Django形式“正好采用1个参数(给定2个)"错误-可能与CSRF有关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆