Django form “takes exactly 1 argument (2 given) ”

2019-08-29 05:44发布

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条回答
霸刀☆藐视天下
2楼-- · 2019-08-29 06:23

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.

查看更多
登录 后发表回答