django表单获取关键字参数的多个值 [英] django form got multiple values for keyword argument
本文介绍了django表单获取关键字参数的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的模型如下:
I have a simple model as follows:
RATING_CHOICES = zip(range(1, 6), range(1, 6))
class Rating(models.Model):
value = models.IntegerField(choices=RATING_CHOICES)
additional_note = models.TextField(null=True, blank=True)
from_user = models.ForeignKey(User, related_name='from_user')
to_user = models.ForeignKey(User, related_name='to_user')
shared_object = models.ForeignKey(ObjectDetail, null=True, blank=True)
dtobject = models.DateTimeField(auto_now_add=True)
从上面的模型我生成一个模型窗体,在我的forms.py中如下:
From the above model I generate a model form, in my forms.py as follows:
class RatingForm(ModelForm):
class Meta:
model = Rating
exclude = ('from_user', 'dtobject',
'shared_object')
在我的网址中,我尝试以下内容:
In my urls I try the following:
url(r'^rate/(?P<form_type>[\w]+)/(?P<oid>\d+)/(?P<oslug>[\w-]+)/$', 'rating_form', name='rating_form'),
在我看来,以下内容:
def rating_form(form_type = None, oid = None, oslug=None):
print form_type
form = RatingForm(data=request.POST or None)
if request.POST and form.is_valid():
form.save()
return HttpResponseRedirect("/")
else:
return render(request, "share.html", {'form' : form })
这样做会给我以下错误:
Doing this gives me the following error:
rating_form()获得了关键字参数'form_type'的多个值
其他详细信息:
Request Method: GET
Request URL: http://127.0.0.1:8000/rate/lending/3/random-stuff/
Django Version: 1.4.1
Exception Type: TypeError
Exception Value:
rating_form() got multiple values for keyword argument 'form_type'
Exception Location: /Library/Python/2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view, line 20
Python Executable: /usr/bin/python
做错了?
推荐答案
您的观点的第一个参数应为请求
the first argument to your view should be request
这篇关于django表单获取关键字参数的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文