我是django开发的初学者,我一直遇到问题,我一直在寻找类似的问题,但没有一个可以解决我的问题。
HTML中没有表单的字段呈现,但按钮呈现正常
我的表格:
from django import forms
from .models import Aluno
class NovoAluno(forms.Form):
class Meta:
model = Aluno
nome = forms.CharField(min_length=15, max_length=100)
direccion = forms.CharField(min_length=10, max_length=250)
ciudad = forms.CharField(min_length=3, max_length=50)
provincia = forms.CharField(min_length=4, max_length=50)
comunidad = forms.CharField(min_length=4, max_length=50)
cp = forms.IntegerField()
faixas = ['Blanco', 'Gris', 'Amarilla', 'Naranja', 'Verde', 'Azul', 'Roxa', 'Marrón', 'Preta']
graduacion = forms.ChoiceField(choices=faixas)
inicio = forms.DateInput()
nacimento = forms.DateInput()
lic = ['Basica', 'Completa']
licencia = forms.ChoiceField(choices=lic)
documento = forms.CharField(min_length=4, max_length=9)
email = forms.EmailField(min_length=10)
profesor = forms.CharField(min_length=5, max_length=100)
centro = forms.CharField(min_length=5, max_length=50)
我的观点:
from django.shortcuts import render, get_object_or_404, redirect
from .forms import NovoAluno
from .models import Aluno
def home(request):
return render(request, 'academia/home.html', {})
def novo(request):
if request.method == "POST":
form = NovoAluno()
if form.is_valid():
form.save()
return redirect('aluno_detalhes', pk=form.pk)
else:
form = NovoAluno()
return render(request, 'academia/cadastro.html', {'form': form})
def pesquisar(request):
return render(request, 'academia/pesquisa.html', {})
def aluno_detalhe(request, pk):
aluno = get_object_or_404(Aluno, pk=pk)
return render(request, 'academia/aluno.html', {'aluno': aluno})
我的模特:
from django.db import models
class Aluno(models.Model):
nome = models.CharField(max_length=100)
direccion = models.CharField(max_length=250)
ciudad = models.CharField(max_length=50)
provincia = models.CharField(max_length=50)
comunidad = models.CharField(max_length=50)
cp = models.IntegerField()
nacimento = models.DateField()
inicio = models.DateField()
documento = models.CharField(max_length=9)
email = models.EmailField()
profesor = models.CharField(max_length=100)
centro = models.CharField(max_length=50)
graduacion = models.CharField(max_length=10)
licencia = models.CharField(max_length=8)
表单的内容块
{% block content %}
<h1>Novo Aluno:</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Cadastrar!</button>
</form>
{% endblock %}
嗯,提前谢谢,如果这是一个愚蠢的错误,我很抱歉,但我不能 看看它在哪里......
答案 0 :(得分:1)
看起来你正在混合form.Form和forms.ModelForm。尝试更改为这样的模型表单:
from django import forms
class NovoAluno(forms.ModelForm):
class Meta:
model = Aluno
fields = '__all__'
或者如果您只想要特定字段:
class NovoAluno(forms.ModelForm):
class Meta:
model = Aluno
fields = [
'nome',
'direcction',
'ciudad',
]
或使用“forms.Form”删除您已有的Meta和模型类(并修复缩进):
class NovoAluno(forms.Form):
nome = forms.CharField(min_length=15, max_length=100)
direccion = forms.CharField(min_length=10, max_length=250)
ciudad = forms.CharField(min_length=3, max_length=50)
provincia = forms.CharField(min_length=4, max_length=50)
comunidad = forms.CharField(min_length=4, max_length=50)
cp = forms.IntegerField()
faixas = ['Blanco', 'Gris', 'Amarilla', 'Naranja', 'Verde', 'Azul', 'Roxa',
'Marrón', 'Preta']
graduacion = forms.ChoiceField(choices=faixas)
inicio = forms.DateInput()
nacimento = forms.DateInput()
lic = ['Basica', 'Completa']
licencia = forms.ChoiceField(choices=lic)
documento = forms.CharField(min_length=4, max_length=9)
email = forms.EmailField(min_length=10)
profesor = forms.CharField(min_length=5, max_length=100)
centro = forms.CharField(min_length=5, max_length=50)
编辑:上面提到的一切都很好,除非在定义选项时,它们应该是2元组对。你可以阅读here。这样你就不会得到“ValueError:太多值来解包(预期2)”
Class NovoAluno(forms.Form):
#code suppressed
faixas = (('1', 'Blanco'), ('2', 'Gris'), ...
..., ('9', 'Marrón'), ('10', 'Preta'))
graduacion = forms.ChoiceField(choices=faixas)
...
etc
答案 1 :(得分:1)
问题解决了!!
itemBuilder
删除了Meta类,然后意识到forms.ChoiceField的列表格式不正确,为元组更改,一切正常!
致@brandondavid