Django forms (validation)

Django forms (validation)

Postby Crimson King » Thu Dec 05, 2013 7:49 pm

I've been reading the django docs and trying to create a small app to fool around.

I created a ModelForm based on my Empadronado model.

On my models.py i used the RegexValidator to create some simple regex patterns so the user can't submit numbers on a name field.

I read in the docs that this ensures the form will validate server-side, what would be the correct way to add some client-side validation?

Code: Select all
#views.py
def creacion_padron(request, documento):
    try:
        padron = model_to_dict(
                Empadronado.objects.filter(documento=documento)[0])
    except IndexError:
        padron = None

    if request.method == 'POST':
        form = EmpadronadoForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.id = padron['id']
            instance.save()
            return HttpResponseRedirect(reverse(
                                    'xxx.yyy.views.gracias'))
    else:
        form = EmpadronadoForm(initial=padron)
    return render_to_response('creacion_padron.html',
                                  {'form': form},
                                  context_instance=RequestContext(request))


Code: Select all
#forms.py
from django.forms import ModelForm
from models import Empadronado

class EmpadronadoForm(ModelForm):
    class Meta:
        model = Empadronado


Code: Select all
#creacion_padron.html
<form method="post" action="">
   {% csrf_token %}
    {{ form.as_p }}
<input type="submit" value="Submit">
</form>
User avatar
Crimson King
 
Posts: 124
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Return to Web Development

Who is online

Users browsing this forum: No registered users and 1 guest