Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

request.GET.get(' ') returns None

I have the following URL: http://127.0.0.1:8000/application_form/Network%20Adminstrator/
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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading