krish krish -4 years ago 89
Python Question

Django form not getting submitted facing attribute error

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download