1

I am trying to store a field value from a webform to django database, my models.py and views.py are given below. I am facing AttributeError: 'WSGIRequest' object has no attribute 'get' * error I am new to django and python, please let me know my mistake here.

VIEWS.PY

MODEL.PY

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Vincent
  • 51
  • 1
  • 8

2 Answers2

0

Edit your view,

from django.shortcuts import redirect, render

def home(request):
    return render(request, 'home.html')

def index(request):
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            form.save()
        #No need to call index view again,
        #it's already been called.
            return redirect('home') #redirection.
    else:
        form = PageForm()
    return render(request, 'index.html', {'form':form})

urls.py

from . import views

url(r'^index/$', views.index, name='index'),
url(r'^home/$', views.home, name='home')

You could use render() shortcut function,

https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render

render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])

render() is a brand spanking new shortcut for render_to_response in 1.3 that will automatically useRequestContext that I will most definitely be using from now on.

The action part in your html form, is where you define the endpoint or url to where the form is submitting. Since, your view accepts the post data, then your action='' needs to be kept empty, which means you are posting to the same view itself.

zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • In my index.html what should i use for the action part? I am getting a404 error after clicking submit. Where should i redirect the page? – Vincent Jun 11 '17 at 17:56
  • You may need to write another view and redirect to that view, I could show you an example, in my answer – zaidfazil Jun 11 '17 at 17:58
  • Please show. And how is the feild value savde to db if i am redirecting it to a new view. I dont find the value in db afvter submission. – Vincent Jun 11 '17 at 17:59
  • For that you need to show your forms.py, but please copy paste the code into the question, don't add link to an image. I have updated my answer – zaidfazil Jun 11 '17 at 18:04
  • You just need to put the `action=''` in the template. – zaidfazil Jun 11 '17 at 18:06
  • I have added my forms.py to questions comment and I am able to store the value to db now, but getting namerror once submitting form – Vincent Jun 11 '17 at 18:10
  • Couple things to remember next time, if you are adding additional information about the question, then edit the question and add it. Don't post it in the comments. Comments are there for their purpose. Your code need to be in the question itself. – zaidfazil Jun 11 '17 at 18:12
  • Yeah sure, I am new here and didn't find the edit button. Now this makes some sense to me. The error is rectified. Thanks for the help. How can I use ajax here without redirection. Just tell me a proper way to achieve it, no need to code for me. Is the ajax need to be written on front end here or does django have anything for this? – Vincent Jun 11 '17 at 18:18
  • You could write an ajax function in the front end and write a corresponding view in the django, like the above mentioned view. – zaidfazil Jun 11 '17 at 18:20
  • I have one more question for you. I have a HTML file basically a web form with me which is developed using bootstrap, jQuery(without using django) and I need to connect this to django. Can I do this? If yes any hints please – Vincent Jun 11 '17 at 18:26
  • Yes, you can... You could grab the data posted from `request.POST` in the view. – zaidfazil Jun 11 '17 at 18:28
  • Refer the documentation for more, actually everything is clearly documented in django documentation – zaidfazil Jun 11 '17 at 18:29
  • Don't I need to use forms.py then? Can you elaborate – Vincent Jun 11 '17 at 18:30
  • Forms are just tools for handling multiple datas. By reading more about django forms, I think you would understand. Forms are not mandatory, but they could become useful. – zaidfazil Jun 11 '17 at 18:33
  • https://stackoverflow.com/questions/10773144/how-to-send-url-parameter-in-post-request-without-form refer this question – zaidfazil Jun 11 '17 at 18:34
  • Yes I am on the way reading the documentation, but its taking time and this is an immediate requirement for me. So posted it here without wasting time. I am yet to connect all the dots, may be I can once I complete the documentation atleast – Vincent Jun 11 '17 at 18:34
  • how can i use templating in already developed html page, I mean the example we discussed here uses some templating in index.html, but in my html file created using bootstrap and jq, how can i use this templating?? simply how should i connect it ? Should i include this templating in my html file? if so how? consider an input field in html, how can i send its value entered by user to backend] – Vincent Jun 11 '17 at 19:42
0

In your views.py there is an error in the context part, {'form':form} itself is the context there is no need to include 'context' again and also include the request render(request,'index.html',{'form':form})

  • In my index.html what should i use for the action part? I am getting a404 error after clicking submit. Where should i redirect the page? – krish 1 min ago edit – Vincent Jun 11 '17 at 17:58