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 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
def creacion_padron(request, documento):
        padron = model_to_dict(
    except IndexError:
        padron = None

    if request.method == 'POST':
        form = EmpadronadoForm(request.POST)
        if form.is_valid():
            instance =
   = padron['id']
            return HttpResponseRedirect(reverse(
        form = EmpadronadoForm(initial=padron)
    return render_to_response('creacion_padron.html',
                                  {'form': form},

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

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

Code: Select all
<form method="post" action="">
   {% csrf_token %}
    {{ form.as_p }}
<input type="submit" value="Submit">
RIP #!
User avatar
Crimson King
Posts: 163
Joined: Fri Mar 08, 2013 2:42 pm
Location: Dublin, Ireland

Return to Web Development

Who is online

Users browsing this forum: No registered users and 4 guests