1
我試圖更新具有外鍵字段的視圖的記錄,由於這我得到一個錯誤,因爲我試圖更新另一個沒有外鍵字段的模型,它工作得很好。錯誤通用細節視圖必須用對象PK或子彈調用,即使使用pk
還有其他的類似這樣的排隊,但在我的情況下,我通過PK。
urls.py
urlpatterns = [
url(r'^info/(?P<studentpk>\d+)/update/$', views.updatestudent.as_view(), name="updatestudent"),
]
views.py
class updatestudent(UpdateView):
model = Student
form_class = forms.studentform
template_name = "temp/updatestudent.html"
def get_success_url(self):
return reverse("courses")
updatestudent.html
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" />
</form>
的models.py
class Student(models.Model):
classfk = models.ForeignKey(Class)
name = models.CharField(max_length=100)
birth_date = models.DateField('Birthdate')
def __str__(self):
return self.name
錯誤
AttributeError: Generic detail view updatestudent must be called with either an object pk or a slug.
它的工作原理:DD,但還有一個問題,我使用了''studentpk'',因爲我已經使用了另一個參數,甚至改變''studentpk''爲P我有問題,可以有沒有同名的參數? –
Goun2
對不起,我不明白你評論中的問題。 – Alasdair
url(r'^ info /(?P \ d +)/(?P \ d +)/ $ 可以重複嗎? url(r'^ info /(?P \ d +)/(?P \ d +)/ $ 但他們持有不同的值 –
Goun2