Hola, intento seguir el ejemplo de ModalForm de la documentacion oficial de django pero creo que ya lo tengo todo y tengo un error que no se como solucionar,
El error es al mostrar el formulario, y dice:
----------------------------------------
NameError at /new_book/
name 'TITLE_CHOICES' is not defined
Request Method: GET
Request URL:
http://127.0.0.1:8000/new_book/
Django Version: 1.3.1
Exception Type: NameError
Exception Value:
name 'TITLE_CHOICES' is not defined
Tengo los codigos como sigue:
cms/mysite/models.py
Código Python:
Ver originalfrom django.db import models
from django.forms import ModelForm
TITLE_CHOICES = (
('MR', 'mr.'),
('MRS','Mrs.'),
('MS','Ms.'),
)
class Author(models.Model):
name = models.CharField(max_length=100)
title = models.CharField(max_length=3, choices=TITLE_CHOICES)
birth_date = models.DateField(blank=True, null=True)
def __unicode__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
class AuthorForm(ModelForm):
class Meta:
model = Author
class BookForm(ModelForm):
class Meta:
model = Book
cms/mysite/forms.py
Código Python:
Ver originalfrom django import forms
class AuthorForm(forms.Form):
name = forms.CharField(max_length=100)
title = forms.CharField(max_length=3,
widget=forms.Select(choices=TITLE_CHOICES))
birth_date = forms.DateField(required=False)
class BookForm(forms.Form):
name = forms.CharField(max_length=100)
authors = forms.ModelMultipleChoiceField(queryset=Author.objects.all())
cms/mysite/views.py
Código Python:
Ver original# Create your views here.
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from forms import BookForm
def new_book(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/new_book/thanks/')
else :
form = BookForm()
return render_to_response('new_book.html',{'form':form,})
y las urls los tengo asi:
Código Python:
Ver originalfrom cms.mysite import views
urlpatterns = patterns('',
(r'^new_book/$', views.new_book),
)
----------------------------
cms/mysite/templates/new_book.html
Código HTML:
Ver original<form action="/set_new_book/" method="post"> {{ form.name}}
{{ form.authors }}