I am working on a weather app where the client will be entering the name of the city in a search bar, and the name will be used to gain weather data from an API. To get the name of the city from the form, I have created this condition in view:
if request.method == 'POST':
city = request.data
print(city)
however when being printed, this error occurs: 'WSGIRequest' object has no attribute 'data'
What am I doing wrong? Or is there a completely different way to do this?
you must get data from template like this: (name of function in views.py is something same as in url.py.
def something(request):
if request.method == 'POST':
city = request.POST.get('data')
print(city)
data is the name of your input tag in template:
<input name='data'>
Update: you need to have a action for your form. it includes a url. this url connect you to your view function for this form. also you must add name to your input tag not your form tag.
template:
<form method="POST" action = "{% url "url_something" %}" >{% csrf_token %}
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" type="text" placeholder="City Name" name="data" >
</div>
<div class="control">
<button class="button is-info" type="submit">
Search City
</button>
</div>
</div>
</form>
in url.py you must have a url like this.:
url(r'^something', views.something, name='url_something'),