21
我有一個簡單的模型多值如下:Django的形式得到了關鍵字參數
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如下:
class RatingForm(ModelForm):
class Meta:
model = Rating
exclude = ('from_user', 'dtobject',
'shared_object')
在我的網址我嘗試以下方法:
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 })
這樣做給了我以下錯誤:
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
我在做什麼錯誤?
*殺我這個*,花了3 f'in小時的奮力什麼是錯的! – whatf
這麼簡單,但我確信這爲一千人節省了一天。 – jball037
我一次又一次地回答這個問題。謝謝。 – ioanb7