将请求 URL 参数传递到创建视图
Pass request URL parameter into create view
我希望这是一个简单的调整。我一直在阅读这里的各种主题,但我错过了一个非常基本的步骤——如何从 URL 请求中获取参数?
例如,URL是http://127.0.0.1:8000/registration/student/item/create/8
URL定义为path('student/item/create/<int:pk>',views.CreateStudentBehaviorItem.as_view(),name='student_item_create'),
我的class观点如下:
class CreateStudentBehaviorItem(LoginRequiredMixin, generic.CreateView):
model = StudentItem
form_class = StudentItemForm
success_url = reverse_lazy('registration:student_item_list')
def get_form_kwargs(self):
kwargs = super(CreateStudentBehaviorItem, self).get_form_kwargs()
kwargs['pk'] = self.request.GET.get('pk')
return kwargs
表格为:
class StudentItemForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.course = kwargs.pop('pk', 'all')
super().__init__(*args, **kwargs)
# Begin filtering by course ID
class Meta:
model = StudentItem
fields = ("item_student","behavior_category","behavior_item","response","internal_comments")
widgets = {
'item_student': forms.Select(attrs={'class': 'form-control'}),
'behavior_category': forms.Select(attrs={'class': 'form-control'}),
'behavior_item': forms.Select(attrs={'class': 'form-control'}),
'response': forms.Select(attrs={'class': 'form-control'}),
'internal_comments': forms.Textarea(attrs={'class': 'form-control', 'rows':5}),
}
我什至还没到过滤的地步,因为我似乎无法从请求中获取 PK URL。我最基本的 Django 经验是使用 get_context_data 获取请求 URL 详细信息。我希望这是一个简单的修正来获取价值。
非常感谢。
试试这个
https://docs.djangoproject.com/en/4.0/ref/urls/#path
def get_form_kwargs(self):
kwargs = super(CreateStudentBehaviorItem, self).get_form_kwargs()
kwargs['pk'] = self.kwargs.get('pk')
return kwargs
我希望这是一个简单的调整。我一直在阅读这里的各种主题,但我错过了一个非常基本的步骤——如何从 URL 请求中获取参数?
例如,URL是http://127.0.0.1:8000/registration/student/item/create/8
URL定义为path('student/item/create/<int:pk>',views.CreateStudentBehaviorItem.as_view(),name='student_item_create'),
我的class观点如下:
class CreateStudentBehaviorItem(LoginRequiredMixin, generic.CreateView):
model = StudentItem
form_class = StudentItemForm
success_url = reverse_lazy('registration:student_item_list')
def get_form_kwargs(self):
kwargs = super(CreateStudentBehaviorItem, self).get_form_kwargs()
kwargs['pk'] = self.request.GET.get('pk')
return kwargs
表格为:
class StudentItemForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.course = kwargs.pop('pk', 'all')
super().__init__(*args, **kwargs)
# Begin filtering by course ID
class Meta:
model = StudentItem
fields = ("item_student","behavior_category","behavior_item","response","internal_comments")
widgets = {
'item_student': forms.Select(attrs={'class': 'form-control'}),
'behavior_category': forms.Select(attrs={'class': 'form-control'}),
'behavior_item': forms.Select(attrs={'class': 'form-control'}),
'response': forms.Select(attrs={'class': 'form-control'}),
'internal_comments': forms.Textarea(attrs={'class': 'form-control', 'rows':5}),
}
我什至还没到过滤的地步,因为我似乎无法从请求中获取 PK URL。我最基本的 Django 经验是使用 get_context_data 获取请求 URL 详细信息。我希望这是一个简单的修正来获取价值。
非常感谢。
试试这个
https://docs.djangoproject.com/en/4.0/ref/urls/#path
def get_form_kwargs(self):
kwargs = super(CreateStudentBehaviorItem, self).get_form_kwargs()
kwargs['pk'] = self.kwargs.get('pk')
return kwargs