无法在Djang中显示我的CreateForm

2024-05-18 19:23:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

校友2.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 %}

表格.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'}),
        }

视图.py

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

网址.py

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

型号.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只显示save按钮,不加载create表单。html是在模板文件夹这就是为什么我有这个网址

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

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

我不确定我是否需要把这个放在url.py中,所以现在我只有这个视图,没有这个就找不到我的模板


Tags: pyformfieldsmodelshtmlformsattrscontrol
1条回答
网友
1楼 · 发布于 2024-05-18 19:23:22

拆下管路

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'

根据documentationFormView替换CreateView

相关问题 更多 >

    热门问题