Hi guys I have a little problem with Django 1.9. the class CreateView me not recorded in the database , the funny thing is that I did not throw any error, however added a code that validates form and asked to make a print (" Bad" ) console if the form is invalid. Every time sending the form (using POST) does not record anything and console, thanks to the setting you did, I printed 'bad ' . Now the problem is that the form is invalid, but can not find what is . Any ideas?
forms.py
from django import forms
from .models import strategies
class FormCreateStgs(forms.ModelForm):
ttlact = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Las letras gritonas',
'required': True, }))
cmpo = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Escribe y selecciona...',
'required': True, }))
aspc = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Escribe y selecciona...',
'required': True, }))
cpmt = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Escribe y selecciona...',
'required': True, }))
apzex = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Escribe y selecciona...',
'required': True, }))
espc = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text', }))
mtrl = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Las letras gritonas',
'required': True, }))
sgcs = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Las letras gritonas',
'required': True, }))
obsv = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Las letras gritonas',
'required': True, }))
iecnc = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Las letras gritonas',
'required': True, }))
ieacvl = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Las letras gritonas',
'required': True, }))
iehbdz = forms.CharField(
widget = forms.TextInput(
attrs={
'class': 'fs-anim-lower',
'type': 'text',
'placeholder': 'Las letras gritonas',
'required': True, }))
actini = forms.CharField(
widget = forms.Textarea(
attrs={
'class': 'fs-anim-lower',
'name': 'q12',
'type': 'text',
'placeholder': 'Las letras gritonas',
'style': 'margin-bottom: 20px;',
'required': True, }))
actdes = forms.CharField(
widget = forms.Textarea(
attrs={
'class': 'fs-anim-lower',
'name': 'q13',
'type': 'text',
'placeholder': 'Las letras gritonas',
'style': 'margin-bottom: 20px;',
'required': True, }))
actfnl = forms.CharField(
widget = forms.Textarea(
attrs={
'class': 'fs-anim-lower',
'name': 'q14',
'type': 'text',
'placeholder': 'Las letras gritonas',
'style': 'margin-bottom: 20px;',
'required': True, }))
class Meta:
model = strategies
exclude = ['ipU', 'nbrchg']
views.py
#from django.shortcuts import render
from django.views.generic.edit import CreateView
from .models import strategies
from .forms import FormCreateStgs
crpt = 'hujpl\strategies'
class CreateStrategie(CreateView):
model = strategies
template_name = "%s\ctrStrategie.html" % (crpt)
form_class = FormCreateStgs
success_url = '/'
def form_valid(self, form):
print("good")
return super(CreateStrategie, self).form_valid(form)
def form_invalid(self, form):
print("BAD")
return super(CreateStrategie, self).form_invalid(form)
template:
<form id="myform" class="fs-form fs-form-full" autocomplete="off" method="POST"> {% csrf_token %} <ol class="fs-fields"> <li> <label class="fs-field-label fs-anim-upper" for="q1" data-info="Escribe tu tema o titulo">Título</label> {{form.ttlact}} <!--input class="fs-anim-lower" id="q1" name="q1" type="text" placeholder="Las letras gritonas" required/--> </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Campo</label> {{form.cmpo}} </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Competencia</label> {{form.cpmt}} </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Aspecto</label> {{form.aspc}} </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Aprendizaje Esperado</label> {{form.apzex}} </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Sugerencias</label> {{form.sgcs}} </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Observaciones</label> {{form.obsv}} </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Indicador de Evaluación:</label> <label class="fs-field-label fs-anim-upper" for="q2">Conocimientos</label> {{form.iecnc}} <label class="fs-field-label fs-anim-upper" for="q2">Actitudes y Valores</label> {{form.ieacvl}} <label class="fs-field-label fs-anim-upper" for="q2">Habilidades y Destrezas</label> {{form.iehbdz}} </li> <li> <label class="fs-field-label fs-anim-upper" for="q2">Actividad</label> <label class="fs-field-label fs-anim-upper" for="q2">Inicio</label> {{form.actini}} <label class="fs-field-label fs-anim-upper" for="q2">Desarrollo</label> {{form.actdes}} <label class="fs-field-label fs-anim-upper" for="q2">Final</label> {{form.actfnl}} </li> </ol><!-- /fs-fields --> <button class="fs-submit" type="submit">Guardar Actividad</button> </form>
It renders the template as well, even with the css and js .
You have instantiated a ModelForm without reference to the model. Perhaps you should be using a plain form like so:
from django import forms
class MyForm(forms.Form):
# define fields
If you use a ModelForm you have to specify the model in a Meta internal class like so:
from . import models
from django import forms
class MyForm(forms.ModelForm):
class Meta:
model = models.MyModel
fields = ['field1', 'field2',]
Check out https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/ and https://docs.djangoproject.com/en/1.9/ref/forms/api/ for more details. I would strongly recommend mastering the docs as they are world class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.