1 回答

TA貢獻1869條經驗 獲得超4個贊
我將使用以下方法刪除get_context_data并覆蓋:get_objectget_object_or_404
class StudentDetail(DetailView, FormClass):
model = Student
template_name = 'student/member.html'
context_object_name = 'student'
form_class = AddConsultation
def get_object(self, queryset=None):
return get_object_or_404(Student, pk=self.kwargs['pk'], school_year=self.kwargs['school_year'])
其他解決方案可能是:
class StudentDetail(DetailView, FormClass):
model = Student
template_name = 'student/member.html'
context_object_name = 'student'
form_class = AddConsultation
slug_field = 'school_year'
slug_url_kwarg = 'school_year'
query_pk_and_slug = True
但我發現第一個不那么神奇:)
添加回答
舉報