Error in Django form

Asked

Viewed 178 times

1

I’m trying to run a form in Django but it returns this error

form = TopicoForm() 



raise ValueError('ModelForm has no model class specified.')

this is my models

class Topico(models.Model):

    titulo = models.CharField('Título', max_length=100)
    mensagem = models.TextField('Mensagem')
    avatar = models.ImageField(upload_to='core/images', verbose_name='Imagem', blank=True, null=True)
    id_usuario = models.ForeignKey(Usuario)
    id_assunto = models.ForeignKey(Assunto)

    def __str__(self):
        return self.titulo

the form

class TopicoForm(forms.ModelForm):

    class meta:
        model = Topico
        fields = '__all__'

and the views

def novo_post(request):
    if request.POST:
        form = TopicoForm(request.POST)
        if form.is_valid:
            form.save()

    else:
        form = TopicoForm()
    contexto = {
        "form": form
    }
    return render(request, "novo_post.html", contexto)

1 answer

1


Error occurred because you wrote meta instead of Meta, the correct would be:

class TopicoForm(forms.ModelForm):

    class Meta:
        model = Topico
        fields = '__all__'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.