[英]Django does not write to the database from form (not admin)
Hi guys I have a little problem with Django 1.9. 嗨,大家好,我对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.
该类CreateView我没有记录在数据库中,有趣的是我没有引发任何错误,但是添加了一个代码来验证表单,并在表单无效的情况下要求进行打印(“ Bad”)控制台。 Every time sending the form (using POST) does not record anything and console, thanks to the setting you did, I printed 'bad ' .
每次发送表单(使用POST)都不会记录任何内容和控制台,由于您所做的设置,我打印了“ 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 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 . 即使使用css和js,它也会呈现模板。
You have instantiated a ModelForm without reference to the model. 您已经实例化了一个ModelForm而不引用该模型。 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: 如果使用ModelForm,则必须在Meta内部类中指定模型,如下所示:
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. 请查看https://docs.djangoproject.com/zh-CN/1.9/topics/forms/modelforms/和https://docs.djangoproject.com/zh-CN/1.9/ref/forms/api/了解更多详细信息。 I would strongly recommend mastering the docs as they are world class.
我强烈建议您精通世界一流的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.