I have the following URL: http://127.0.0.1:8000/application_form/Network Adminstrator/
The URL is generated with: path('application_form/<str:job_title>/', views.ApplicationForm.as_view(), name='application_form')
I keep on trying get_data = self.request.GET.get('job_title')
and am expecting to get Network Adminstrator
but instead it returns None
My urls.py:
urlpatterns = [
path('application_form/<str:job_title>/', views.ApplicationForm.as_view(), name='application_form'),
]
views.py:
class ApplicationForm(CreateView):
model = Requirements
form_class = ApplicationForm
template_name = 'requirements/job_specs.html'
# Passes the request object to forms
def get_form_kwargs(self):
kwargs = super(ApplicationForm, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
forms.py:
class ApplicationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
super(ApplicationForm, self).__init__(*args, **kwargs)
get_data = self.request.GET.get('job_title')
print(get_data)
dic = {'job_title__job_title': get_data}
self.fields['qualifications'].queryset = Requirements.objects.get(**dic)
class Meta:
model = Applicants
fields = ['email', 'qualifications']
email = forms.EmailField(label='', max_length=100, required=True, widget=forms.TextInput(
attrs={'class': 'form-group form-control input-lg ', 'placeholder': 'Email'}), )
qualifications = forms.ModelMultipleChoiceField(queryset=None, widget=forms.CheckboxSelectMultiple)
Any thoughts or ideas? I am still a bit new to programing in Django and I would really appreciate your time and knowledge.
CodePudding user response:
URL kwargs are not in request, but in class itself. Try self.kwargs.get('job_title')
. If it's not in your ApplicationForm, you can pass them from get_form_kwargs
method same as you passed Request