Wednesday, February 27, 2013

Re: pin matching query does not exist

A good place to start would be to fix the fact that, if the form is not valid on a post, the view returns None.

Bill

On Wed, Feb 27, 2013 at 4:53 AM, okorie Emmanuel <nickson1301@gmail.com> wrote:
hi

Am designing a form from django model, i have store some information in the database already, each time i enter a correct values am redirected to another page,but when ever an empty field is submitted it crash with this message " pin matching query does not exist" also when wrong query is entered (that is queries not in the database  ) the same thing happens that is" pin matching query does not exist" . I think validation is the problem but i don't know how to go about it. here is my code  

form.py

from django import forms
from you.models import Pin
from django.forms import ModelForm

class PinForm(forms.ModelForm):
   
    class Meta:
        #pin = forms.CharField(max_length=12, widget=forms.PasswordInput())
        model = Pin
        fields = ('serial_no','pin')
        #exclude = ('is_active',)
        widgets = {'pin': forms.PasswordInput(render_value=False),}
        def clean(self):
            from django.core.exceptions import ValidationError
            if self.pin != pin  and self.serial_no != serial_no:
                raise ValidationError('pin does not exist')
            if self.pin == pin and self.serial_no and self.is_active == False:
                raise ValidationError('pin has being used')
            if self.pin == ""  and self.serial_no == "":
                raise ValidationError('pin and serial no field cannot be empty')


model.py

from django.db import models
from django.forms import ModelForm
from django.core.exceptions import ValidationError


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


views.py

from you.forms import PinForm 
from django.contrib.auth import authenticate, login, REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.conf import settings
from you.models import Pin
from django import forms
from django.core.exceptions import ValidationError


def index(request, template_name="index.html"):
    ValidationError=[]
    if request.method == "POST":
        postdata = request.POST.copy()
        form = PinForm(postdata)
        if form.is_valid():
            pin = request.POST.get('pin', '')
            serial_no = request.POST.get('serial_no', '')
            #decline = Pin.objects.get(pin=pin, serial_no=serial_no, is_active=False)
            accept = Pin.objects.get(pin=pin, serial_no=serial_no, is_active=True)
            if accept is not None and accept.is_active:
                return HttpResponseRedirect('/acceptance/')
           
    else:
        form = PinForm()
        return render_to_response('index.html',{'ValidationError':ValidationError, 'form':form},context_instance=RequestContext(request) )
             
   
           
               
def acceptance(request, template_name = "acceptance.html"):
    return render_to_response(template_name, locals(),context_instance = RequestContext(request))



Template index.html

{% extends 'base.html'%}

{% block site_wrapper %}
        <h1>Register</h1>
    {% if errors %}
        <ul>
            {% for ValidationError in ValidationError %}
            <li>{{ ValidationError }}</li>
            {% endfor %}
        </ul>
    {% endif %}
   
    <form action="" method="post">{% csrf_token %}
          
        <table >
         {{ form.as_table }}
        <tr>
            <th></th>
            <td>
                <input type="submit" name="submit" value="Register" />
               
            </td>
            <input type="hidden" name="next" value="{{ next }}" />
        </tr>
        </table> 
             
    </form>


{% endblock %}



pls how can i validate this model form so that when an empty form is send it will display "can't submit empty form  to the user", when wrong query is entered it will display to the user "pin and serial number does not exist" and if the right query is entered and the is_active is false it should tell the user "pin used ", this is because i intend to deactivate a pin and serial number when a user registers successfully. thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment