Django-fr

Forum

#1 14-12-2012 18:58:20

car00x
Membre
Inscription : 31-05-2012
Messages : 39

verification des champs du formulaire

Bonjour la communauté,
j'ai un problème de verification des champs de mon formulaire quand je valide, je m'explique:
quand je valide le formulaire,la vérification par defaut de django marche c'est à dire que les erreures s'affiche  mais ne tient pas compte du  système de vérification que j'ai mis en place ne marche, je ne sais pas si j'ai fait des erreures quelques, prière jetter un coup d'oeil sur mon code s'il vous plait.
merci
voici mon code de views:

from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.utils.translation import ugettext as _
from django import forms
from django.shortcuts import render_to_response, redirect
from django.utils.hashcompat import md5_constructor, sha_constructor
from django.forms import ModelForm
from django import forms
from models import Tout
import django.utils.simplejson as json

def index(request):
    return render_to_response('index.html')

class Doctor(ModelForm):
    
    class Meta:
       model = Tout
       widgets = {
            'password': forms.PasswordInput(),'password2': forms.PasswordInput()
        }

       default_error_messages = {
        'already exists': _(u'A user with that username already exists'),
        'already exists': _(u'Already use , choise another please'),
        'short_password': _(u'Password has to be at least 8 characters long'),
        'is not the same password': _(u'The two password fields didn\'t match'),
        'required': _(u'you must choice your sex.'),
        'required': _('you must choice your countrie'),
        'required': _(u'your town please.'),
        'required': _('you must choice your speciality'),
    }

def clean_username(self):
        username = self.cleaned_data["username"]
     
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.default_error_messages['Already existe'])
    
def clean_email(self):
        """
        Validates that an active user exists with the given e-mail address.
        """
        email = self.cleaned_data["email"]
        self.users_cache = User.objects.filter(
                                email__iexact=email,
                                is_active=True
                            )
        if len(self.users_cache) == 1:
            raise forms.ValidationError(self.default_error_messages['Already existe'])
        return email
    
def clean_password(self):
        password = self.cleaned_data['password']
        length = len(password)
        if length < 8:
            raise forms.ValidationError(self.default_error_messages['short password'])
        return password

def clean_password2(self):
    # Make sure the new password and confirmation match
    password = self.cleaned_data.get('password')
    password2 = self.cleaned_data.get('password2')
     
    if password != password2:
        raise forms.ValidationError(self.default_error_messages['it is not the same password']) 
    return password2
    
def account(request):
    if request.method == 'POST':  
        form_d = Doctor(request.POST)

        if form_d.is_valid():
           username = form_d.cleaned_data['username']
           last_name = form_d.cleaned_data['last_name']
           first_name = form_d.cleaned_data['first_name']
           email = form_d.cleaned_data['email']
           password = form_d.cleaned_data['password']
           password2 = form_d.cleaned_data['password2']
           sex = form_d.cleaned_data['sex']
           country = form_d.cleaned_data['country']
           town = form_d.cleaned_data['town']
           speciality = form_d.cleaned_data['speciality']
           form_d.save()
           
           if request.is_ajax():
                return HttpResponse(content=json.dumps({'success' : '/success'}), mimetype='application/json')

           return redirect('success') # Redirect after POST
        elif request.is_ajax():
                errors = json.dumps(forms.errors)
                return HttpResponse(errors, mimetype='application/json')
    else: 
        form_d = Doctor()
    return render_to_response('account.html', {'form_d':form_d})

def clean_password(self):
        password = self.cleaned_data['password']
        length = len(password)
        if length < 8:
            raise forms.ValidationError("Password has to be at least 8 characters long.")
        return password

class Patience(ModelForm):
    class Meta:
       model = Tout
       widgets = {
            'password': forms.PasswordInput(),'password2': forms.PasswordInput()
        }
       fields = ('username', 'first_name', 'last_name', 'email',  'password', 'password2','sex', 'country', 'town', )
         
def patient(request):
    if request.method == 'POST':  
        form_pa = Patience(request.POST) 
        if form_pa.is_valid():
           username = form_d.cleaned_data['username']
           last_name = form_pa.cleaned_data['last_name']
           first_name = form_pa.cleaned_data['first_name']
           email = form_pa.cleaned_data['email']
           password = form_pa.cleaned_data['password']
           password2 = form_pa.cleaned_data['password2']
           sex = form_pa.cleaned_data['sex']
           country = form_pa.cleaned_data['country']
           town = form_pa.cleaned_data['town']
           form_pa.save()
           return HttpResponseRedirect('success')
    else: 
        form_pa = Patience()
    return render_to_response('patient.html', {'form_pa' : form_pa}, context_instance=RequestContext(request))
def save   
def success(request):
    return render_to_response('success.html', {'success' : success}, context_instance=RequestContext(request)) 

def login(request):
    
    return render_to_response('login.html', {'login' : login}, context_instance=RequestContext(request))

Hors ligne

#2 15-12-2012 10:05:49

torrak
Membre
Inscription : 31-08-2011
Messages : 47

Re : verification des champs du formulaire

N'aurais tu pas une erreur de syntaxe niveau du 'already exists' car dans la suite de ton code il est orthographié comme ceci 'Already existe'

Hors ligne

#3 15-12-2012 11:45:20

car00x
Membre
Inscription : 31-05-2012
Messages : 39

Re : verification des champs du formulaire

non je n'ai aucun message d'erreure

Hors ligne

Pied de page des forums