Django form “takes exactly 1 argument (2 given) ” error - possibly related to CSRF?

杀马特。学长 韩版系。学妹 提交于 2020-01-17 02:26:32

问题


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

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

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

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

Here's the actual code:

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))

The 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>

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/5689714/django-form-takes-exactly-1-argument-2-given-error-possibly-related-to-cs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!