Created
September 20, 2012 20:50
-
-
Save fabiocerqueira/3758287 to your computer and use it in GitHub Desktop.
Exemplo View com forms por parâmetro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% extends 'base.html'%} | |
| {% block content %} | |
| {% if messages %} | |
| <ul class="messages"> | |
| {% for message in messages %} | |
| <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> | |
| {% endfor %} | |
| </ul> | |
| {% endif %} | |
| <form action="" method="post"> | |
| {% csrf_token %} | |
| {{ form.as_p }} | |
| <input type="submit" value="Submit" /> | |
| </form> | |
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django import forms | |
| class FormA(forms.Form): | |
| name = forms.CharField() | |
| def save(self): | |
| pass | |
| class FormB(forms.Form): | |
| email = forms.EmailField() | |
| def save(self): | |
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.conf.urls import patterns, url | |
| from views import create | |
| from forms import FormB, FormA | |
| urlpatterns = patterns('', | |
| url('^form/a$', create, | |
| kwargs={'form_class': FormA, 'success_url': 'forma'}, | |
| name='forma'), | |
| url('^form/b$', create, | |
| kwargs={'form_class': FormB, 'success_url': 'formb'}, | |
| name='formb'), | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.shortcuts import render, redirect | |
| from django.contrib import messages | |
| def create(request, form_class, success_url='/'): | |
| if request.method == 'POST': | |
| form = form_class(request.POST) | |
| if form.is_valid(): | |
| form.save() | |
| messages.success(request, '%s created.' % form_class.__name__) | |
| return redirect(success_url) | |
| else: | |
| form = form_class() | |
| return render(request, 'form_template.html', { | |
| 'form': form, | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment