0

In my Django application, I need to access what the user filled in the City text field.

This is the city text field from the template:

<label class="reglabel">City: </label><br>
{{ profile_form.city|add_class:"input-md textfields" | attr:"placeholder: Search Cities..."|attr:"id:searchBoxGlow" | attr:"name: cityname" }}
{{ profile_form.city.errors }}<br>

In my View.py file I try to access the value using the name attribute I added to the template:

print request.POST.get("cityname", "")

I followed advice from this question:

Django - taking values from POST request

This is the form, at this stage I just want to pull the text the user fills so I can turn it into a city instance and save the form.

class UserProfileForm(forms.ModelForm):
    city = forms.CharField(required=True)
    class Meta:
        model = UserProfile
        fields = ('profilepic', 'city', 'hobbies', 'languages')

However, it prints blank instead of what the user filled in the City text field.

Community
  • 1
  • 1
Seif
  • 566
  • 2
  • 10
  • 21

1 Answers1

0

If I print request.POST I receive the following:

<QueryDict: {u'city': [u'London, United Kingdom'], u'first_name': [u'seifautocomplete'], u'last_name': [u'Elmughrabi'], u'languages': [u'263'], u'hobbies': [u'2110'], u'csrfmiddlewaretoken': [u'aPBSrZgfhHx03xPKF5xc5NtLrPL1r1Bc'], u'profilepic': [u'']}>

In order to pull city I need to use the key which is: 'city' instead of using HTML element name attribute like I did in the question body.

This is what I need to do:

print request.POST.get("city", "")

The issue is resolved.

Seif
  • 566
  • 2
  • 10
  • 21