Champ caché dans un formulaire sous Django

Published: 25 février 2015

DMCA.com Protection Status

Exemple de comment avoir un champ caché dans un formulaire avec Django:

models.py

from django.db import models
from django import forms

class MyForm(forms.Form):
    MyHiddenParameter = forms.CharField(widget=forms.HiddenInput())

urls.py

url(r'^test/$', 'MyApp.views.MyView')

views.py

from MyApp.models import MyForm

def MyView(request,path):

if request.method == 'POST':

        form = MyForm(request.POST)

        if form.is_valid():

            print form.cleaned_data[ 'MyHiddenParameter' ]

    return render_to_response("MyApp/MyTemplate.html", 
         context_instance=RequestContext(request))

MyTemplate.html

<form action="http://127.0.0.1:8000/test/" method="post">
{% csrf_token %}

<input type='hidden' name="MyHiddenParameter" value="4" />

<input type="submit" value="Correction" />
</form>

Recherches associées