无法在Django中显示我的CreateForm

时间:2019-05-30 09:28:02

标签: python html django

我已经在Django中创建了一个表单和一个视图,我试图在HTML中显示它,但是它没有加载任何东西,我也不知道为什么。

alumno2.html

{% block header %}
    <header class="masthead bg-white text-dark text-uppercase">
        <div class="container">
            <h3 class="text-center">Añadir alumno</h3>
            <form method="post">
                {% csrf_token %}
                    {{ form.as_p }}
                <button class="btn btn-secondary" type="submit">Guardar</button>
            </form>
        </div>
    </header>
{% endblock %}

form.py

class AlumnoForm2(forms.ModelForm):
    class Meta:
        model = Alumno
        #fields = ['dni', 'nombre', 'apellido1', 'apellido2','email','repetidor']
        fields = ['dni', 'nombre', 'apellido1', 'apellido2','email','repetidor','curs']

        labels = {
            'dni': 'dni',
            'nombre': 'nombre',
            'apellido1': 'Primer Apellido',
            'apellido2': 'Segundo Apellido',
            'email': 'Email',
            'repetidor': 'repetidor',
            'curs': 'curs'
        }

        widgets = {
            'dni': forms.TextInput(attrs={'class': 'form-control'}),
            'nombre': forms.TextInput(attrs={'class': 'form-control'}),
            'apellido1': forms.TextInput(attrs={'class': 'form-control'}),
            'apellido2': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.TextInput(attrs={'class': 'form-control'}),
            'repetidor': forms.CheckboxInput(attrs={'class':'form-control-checkbox','id': 'repetidor'}),
            'curs':forms.Select(attrs={'class': 'form-control'}),
        }

view.py

class crea_alumno(CreateView):
    model = Alumno
    form_class = AlumnoForm2
    template_name = '/alumno2.html'
    success_url = reverse_lazy('mostrar_alumnos')

url.py

url(r'^alumno2/$', crea_alumno.as_view(),name='alumno2'),

models.py

class Alumno(models.Model):
    dni = models.CharField(max_length=9,primary_key=True)
    nombre = models.CharField(max_length=100)
    apellido1 = models.CharField('Primer apellido',max_length=50)
    apellido2 = models.CharField('Segundo apellido',max_length=50)
    email = models.EmailField("Correo electronico",null=True)
    repetidor = models.BooleanField()
    curs = models.ManyToManyField(Curso, blank=True, related_name="Historico_de_cursos")
    Nivel = models.ManyToManyField('Nivel', through = 'Completado',through_fields=('Alumno','Nivel'))
    Practica = models.ManyToManyField('Practica', through = 'Nota',through_fields=('Alumno','Practica'))
    Curso = models.ManyToManyField('Curso',through = 'Curso_alumno',through_fields=('Alumno','Curso'))


    def __str__(self):
        return self.dni

html仅显示“保存”按钮,未加载创建表单。 html位于模板文件夹中,这就是为什么我有此网址。

编辑: 我已经删除了模板,

url(r'^alumno2/$', TemplateView.as_view(template_name='alumno2.html'),name='alumno2'),

而且我不确定是否需要将其放在url.py中,所以现在我只有视图了,没有它就找不到我的模板

1 个答案:

答案 0 :(得分:0)

删除行

url(r'^alumno2/$', TemplateView.as_view(template_name='alumno2.html'),name='alumno2'),

从您的urls.py开始,只保留其中一个:

url(r'^alumno2/$', crea_alumno.as_view(),name='alumno2'),

在视图内部,删除模板名称中的/:而不是

template_name = '/alumno2.html'

使用

template_name = 'alumno2.html'

并按照documentation

CreateView替换为FormView