大家好,我对Django 1.9有点问题。我没有在数据库中记录类CreateView,有趣的是我没有抛出任何错误,但是添加了一个验证表单的代码并要求打印(" Bad")如果表单是无效。每次发送表单(使用POST)都没有记录任何内容和控制台,由于你所做的设置,我打印了“坏”' 。现在问题是表单无效,但找不到是什么。有什么想法吗?
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)
模板:
<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>
&#13;
它也会渲染模板,即使使用css和js。
答案 0 :(得分:1)
您已经实例化了ModelForm而没有引用模型。也许你应该使用这样的简单形式:
from django import forms
class MyForm(forms.Form):
# define fields
如果使用ModelForm,则必须在Meta内部类中指定模型,如下所示:
from . import models
from django import forms
class MyForm(forms.ModelForm):
class Meta:
model = models.MyModel
fields = ['field1', 'field2',]
查看https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/和https://docs.djangoproject.com/en/1.9/ref/forms/api/了解详情。我强烈建议掌握文档,因为它们是世界级的。