Search code examples
pythondjangoformsdjango-forms

Django ModelForm has no model class specified error


*** forms.py file ***

I've checked most sites and videos and the issue is not solving.

    from django import forms
    from django.forms import ModelForm

    from .models import *

    class TaskForm(forms.ModelForm):

    class Mete:
        model = Task
        fields = '__all__'

  • here is the views.py file *
    from django.shortcuts import render, redirect
    from .models import *
    from .forms import *

    # Create your views here.

    def home(request):
        tasks = Task.objects.all()

        form = TaskForm()

        if request.method == 'POST':
            form = TaskForm(request.POST)
            if form.is_valid():
                form.save()
            return redirect('/')

        context = {'tasks':tasks, 'form':form}
        return render(request, 'home.html', context)

  • here is the html file *
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Home page</title>
    </head>
    <body>
        <h1>Todo project by Rohit.</h1>

        <form method="POST" action="/">
            {% csrf_token %}
            {{ form.title }}
            <input type="submit" name="Create Task">
        </form>

        {% for task in tasks %}

        <div>
            <a href="{% url 'deletepage' %}">Delete task</a>
            <a href="{% url 'updatepage' %}">Update task</a>
            {{task.title}}
        </div>

        {% endfor %}

    </body>
    </html>

Solution

  • You have a typo on your form, its Meta not Mete

       class TaskForm(forms.ModelForm):
           class Meta:
              model = Task
              fields = '__all__'