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
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.



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():
        #No need to call index view again,
        #it's already been called.
            return redirect('home') #redirection.
        form = PageForm()
    return render(request, 'index.html', {'form':form})

from . import views

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

You could use render() shortcut function,

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