0

I am new to django so bear with me.I am trying to build a simple web app to fetch the current weather.Problem is I am not able to fetch the pincode entered in my HTML form and use it in my views.py file. As I told u I am new to django so please try to explain it the most simple way. Thanks.enter image description here

enter image description here

1
  • @I have edited the code of views.py Commented Jul 8, 2017 at 6:44

2 Answers 2

2

Your form has GET method, but in view you are trying to fetch data from POST. Change view code to this:

data=request.GET.get('Location')

Also you shoul move

return HttpResponse('hello')

at the end of view. Code after return will never be called.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry,I Posted the unedited version of views.py.I have already tried "data=request.GET.get('Location')"..It doesn't work
@mathers25 oh I missed return statement in you code. Try to move it at the end of function.
Thanks man!It worked.One more thing..if i want to pass this value to a url to make an api call should i do that in views.py ?
@mathers25 you are welcome! Sure you can pass in in view as url argument using reverse fuction: reverse('url', args=[data]) or you can add it to the url as GET arguments: `{0}?data={1}'.format(reverse('url'), data). You can find details about reverse function here: docs.djangoproject.com/en/1.11/ref/urlresolvers/#reverse
1

if you are using GET in form

in views

location = request.GET.get('Location')
print(location)
return HttpResponse('whatever')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.