开发者

404 Error while processing radio buttons in a form . How to debug?

开发者 https://www.devze.com 2023-03-13 07:22 出处:网络
This is my views.py from django.conf import settings from django.shortcuts import render_to_response from django.template import RequestContext, loader

This is my views.py

from django.conf import settings
from django.shortcuts import render_to_response
from django.template import RequestContext, loader
from django import forms


def TestLayer(request):
    users = User.objects.all()
    if request.method == 'POST': 
            form = TestForm(request.POST) 
            if form.is_valid():
            user = form.cleaned_data['user']
                    rad1=form.cleaned_data['radio1']
                    rad2=form.cleaned_data['radio2']
                test = Permission()
            test.user = user
            test.val = rad1 + rad2 
            test.save()
            return render_to_response('testlayer.html',{'user':users})
        else:
            form = TestForm()



        return render_to_response('testlayer.html', {'user':users})

This is my forms.py

from django import forms
from django.forms.widgets import RadioSelect

class TestForm(forms.Form):
    user = forms.CharField(max_length=100)
    RADIO_CHOICES1 = [['1','Radio 1'],['2','Radio 2']]
    RADIO_CHOICES2 = [['3','Radio 2'],['4','Radio 2']]
    radio = forms.ChoiceField( widget=RadioSelect(), choices=RADIO_CHOICES1)
    radio = forms.ChoiceField( widget=RadioSelect(), choices=RADIO_CHOICES2)

My urls.py is this url(r'^tests/','test.views.TestLayer',name='testlayer'),

When I click submit button the form is either not getting processed or its throwing a 404 error . Is my view correct according to the form and template ? I have generated the temp开发者_开发知识库late from the forms .


There are a few things I noticed about your view and form code...

First, your TestForm class defines "radio" twice, instead of the two fields you're looking for in the form's cleaned data collection in the view: radio1, radio2

Second, you're not passing the form to the template in your view.

Third, there's no need to return render_to_response twice, or to even have the condition where you're creating a new instance of the test form. Instead try this:

#views.py
from django.conf import settings
from django.shortcuts import render #assumes Django 1.3

#these imports seem to be missing...
from your_app.forms import TestForm
from your_app.models import Permission

def test_layer(request):
    users = User.objects.all()
    form = TestForm(request.POST or None)
    if request.method == 'POST': 
        if form.is_valid():
            cleaned_data = form.cleaned_data
            user = cleaned_data.get('user')
            radio1 = cleaned_data.get('radio1')
            radio2 = cleaned_data.get('radio2')
            test = Permission()
            test.user = user
            test.val = radio1 + radio2 
            test.save()
    return render(request, 'testlayer.html', {'user':users, 'form' : form})

#forms.py
from django import forms

class TestForm(forms.Form):
    user = forms.CharField(max_length=100)
    RADIO_CHOICES1 = [['1','Radio 1'],['2','Radio 2']]
    RADIO_CHOICES2 = [['3','Radio 2'],['4','Radio 2']]
    radio1 = forms.ChoiceField( widget=forms.RadioSelect(), choices=RADIO_CHOICES1)
    radio2 = forms.ChoiceField( widget=forms.RadioSelect(), choices=RADIO_CHOICES2)

Also, your URL pattern doesn't end with $, and you can prefix your patterns to avoid repeating the path to your view function(s):

#urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('test.views',
    url(r'^tests/$','test_layer', name='testlayer'),
)

Lastly, your view function name was title-cased: TestLayer. In Python, it's a convention that function names, variables, etc, are lower-cased with underscores separating words, and classes are title-cased.

Hope that helps you out.

0

精彩评论

暂无评论...
验证码 换一张
取 消