Avec Django, il est évidemment possible de mettre en place un formulaire sans être obligé de développer un "model" associé à celui-ci. En effet, il arrive souvent que l'on veuille mettre en place un formulaire sans vouloir stocker dans la base de données les informations envoyées par celui-ci. Ce cas de figure est bien expliqué dans la documentation de Django (voir). Exemple simple de mise en pratique:
models.py:
from django.db import models
from django import forms
class LorenzMieForm(forms.Form):
Wavelength = forms.DecimalField()
SphereRadius = forms.DecimalField()
RealPart = forms.DecimalField()
ImaginaryPart = forms.DecimalField()
views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
from ApplicationsCollection.models import LorenzMieForm
def LorenzMieView(request):
if request.method == 'POST':
form = LorenzMieForm(request.POST)
if form.is_valid():
Submitted_Wavelength = form.cleaned_data[ 'Wavelength' ]
Submitted_SphereRadius = form.cleaned_data[ 'SphereRadius' ]
Submitted_RealPart = form.cleaned_data[ 'RealPart' ]
Submitted_ImaginaryPart = form.cleaned_data[ 'ImaginaryPart' ]
print Submitted_Wavelength, Submitted_SphereRadius, \
Submitted_RealPart, Submitted_ImaginaryPart
# Do something
else:
form = LorenzMieForm()
return render_to_response("ApplicationsCollection/LorenzMieView.html",
{'form':form},
context_instance=RequestContext(request))
template LorenzMieView.html:
<form action="/_LorenzMieView/" method="post" class="form-horizontal">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Submit</button>
</form>
Recherches associées
Liens | Site |
---|---|
Django Forms | Django Doc |
Can I have a Django form without Model | stackoverflow |
Django forms “not” using forms from models | stackoverflow |
Form Basics | effective django |
Django: allow a FloatField or an IntegerField in an input form? | stackoverflow |
Django: DecimalField values displaying | stackoverflow |