How can i redirect a form to amother page when validation is correct

44 views
Skip to first unread message

Okorie Emmanuel

unread,
Jan 23, 2013, 5:19:07 PM1/23/13
to django...@googlegroups.com
Am trying to build an application that user can login to register  using pin and serial number from a scratch card, a valid pin and serial number should take a user to the registration page to create an account, here are codes i have writen 

view.py

def index(request, template_name="index.html"):
    errors=[]
    if request.method == "POST":
        form = PinForm()
        if form.is_valid():
            #form.save()
            #form.cleaned_data
            pin1 = request.POST.get('pin', '')
            serial_no1 = request.POST.get('serial_no', '')
            accept = auth.authenticate(pin=pin1, serial_no=serial_no1)
            if accept is not None and accept.is_active:
        # Correct password, and the user is marked "active"
                auth.login(request, accept)
        # Redirect to a success page.
                return HttpResponseRedirect('/acceptance/')

            
    else:
        form = PinForm()
        #accept = authenticate(pin != pin, serial_no !=serial_no, is_valid = False )
        #errors.append('This card Does Not Exist')
 #       form = PinForm()
    return render_to_response(template_name, locals(),context_instance = RequestContext(request))


def acceptance(request, template_name = "acceptance.html"):
    return render_to_response(template_name, locals(),context_instance = RequestContext(request))

form.py

from django import forms
from you.models import Pin

class PinForm(forms.ModelForm):
    class Meta:
        model = Pin
        exclude = ('is_active',)
        
urls.py

from django.conf.urls.defaults import *


urlpatterns = patterns('you.views',
            (r'^$','index', {'template_name': 'index.html'}, 'home_page'),
            #(r'^acceptance/$',{'template_name': 'acceptance.html'}, 'acceptance'),
            (r'^acceptance/$', 'acceptance', {'template_name': 'acceptance.html'}, 'acceptance'),

and 

models.py

from django.db import models
from django.forms import ModelForm

class Pin(models.Model):
    pin = models.CharField(max_length=12)
    serial_no = models.CharField(max_length=12)
    is_active = models.BooleanField(default=True)
    
    class Meta:
        db_table = 'db_Pin'
        ordering = ['pin']
        def __unicode__(self):
            return self.pin      

              
In the model i have actually stored the pin and serial no, but each time i entered a valid pin and serial number it does not redirect to the next page which for now
is the acceptance page, this i intend to change to the registration page. pls what have i not done right, pls help me fix this. thanks 

Jason Arnst-Goodrich

unread,
Jan 24, 2013, 1:11:41 PM1/24/13
to django...@googlegroups.com
It's not redirecting because the following statement never evaluates to True:

if accept is not None and accept.is_active:


You are using auth.authenticate (see below) but I don't think that's what you want in this case. auth is the built in django auth system which your code isn't using.

accept = auth.authenticate(pin=pin1, serial_no=serial_no1)

It looks like you're instead trying to load a Pin object.

Try this instead:

accept = Pin.objects.get(pin=pin1, serial_no=serial_no1)

Now accept is holding the Pin object (assuming it found one) and you can check that object for the property is_active


(remember to import Pin).

Okorie Emmanuel

unread,
Jan 27, 2013, 9:33:02 PM1/27/13
to django...@googlegroups.com
thanks for your help

 the from redirected when i used
accept = Pin.objects.get(pin=pin1, serial_no=serial_no1)

but again i discover that a user can view the accept page by typing it on his or her url
without following the normal process of entering pin and serial no first

how can i stop this, how can i force a user to see that accept page only
when he/she is comes only from the main form page, what i mean a user
can only be redirected to the accept page only when he has the valid credentials
otherwise he/she cannot view the accept page even if he entered the correct url
on his browser. thanks


Reply all
Reply to author
Forward
0 new messages