Django form is throwing error: "takes exactly 1 argument (0 given)"
I'm working on a basic event form created from a model, but I keep getting the following error message:
TypeError at /addlaundry/
addlaundry() takes exactly 1 argument (0 given)
I think it's because I'm not passing the argument through on views, but I can't find documented anywhere how to do this right, at least not written in a way I understand.
Here is my urls.py:
urlpatterns = patterns('',
url('^addlaundry/$', 'beacon.laundry.views.addlaundry'),
}
And the views itself:
# Create your views here.
f开发者_运维百科rom schedule.views import EventForm
def addlaundry(request):
if request.method == 'POST':
form = EventForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/') #redirect after succesfully adding new delivery
else:
form = addlaundry()
return render_to_response('newlaundry.html', {
'form': form,
})
Do I indeed have my views wrongly structured, or am I missing something else? If there's documentation I need to read up on, I want to I just haven't found it but feel like I'm missing something basic.
Thanks,
Michael
The problem is here:
form = addlaundry()
You're calling your view function addlaundry
which takes 1 required argument (request
), but you're not passing it any arguments.
Of course, that's not the right way to construct a form, anyway. You'll want to take a look at the examples given in the Django forms documentation to see how to create and use forms in Django.
Your view is called addlaundry
, and it calls (presumably) something else called addlaundry
. Rename one of them, or use the other addlaundry
from inside its namespace.
This is your problem:
form = addlaundry()
That's attempting to call the view itself! That's not what you want. You need to define a form class and call (instantiate) it here.
views.py :
from schedule.forms import EventForm
def addlaundry(request):
if request.method == 'POST':
form = EventForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = EventForm()
return render_to_response('newlaundry.html', {
'form': form,
})
That means :
- using a forms.py file to define your forms
- initializing your form in a non post context (regular first page load), that will be passed to your template
Also, in urls.py your view should not be a string.
else:
form = addlaundry()
Just as the exception says: The view function needs 1 argument, but you didn't supply any.
精彩评论