The problem with forms in Django

I tried bros in Django forms, but something weird happened! When I send the form data from views.py file to the newcars.html template file, for some reason the form variable is empty! I don't understand what to do. I've been trying to solve this problem for 3 hours, please help me😢

P.S. It makes no sense to insert code from an HTML file, since the " form " file is empty after the " data " dictionary

views.py :

def newcars(request):  #stackoverflow почему-то не ставит табуляцию, но в коде все норм👌
if request.method == "POST":
    form = AddPostForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('home')

else:
    form = AddPostForm()

data = {
    'title': 'AddPostForm',
    'form': form
}
return render(request, 'hello/newcars.html', data)

forms.py :

from django.core.exceptions import ValidationError
from django import forms

from .models import *

class AddPostForm(forms.Form):
    class Meta:
        model = Cars
        fields = ['title', 'slug', 'content', 'is_published', 'brand']
    

models.py :

from django.urls import reverse
from django.db import models

# Create your models here.

class Cars(models.Model):
    title = models.CharField(max_length=255, verbose_name="Название")
    slug = models.SlugField(verbose_name="URL", unique=True, db_index=True, max_length=255)
    content = models.TextField(verbose_name="Характеристика", null=True)
    time_create = models.DateField(verbose_name='Дата публикации', auto_now_add=True)
    is_published = models.BooleanField(default=True )
    brand = models.ForeignKey('Brands', on_delete=models.PROTECT)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('car_slug', kwargs={'car_slug': self.slug})

    class Meta:
        verbose_name = 'Машины'
        verbose_name_plural = 'Машины'


class Brands(models.Model):
    name = models.CharField(verbose_name="Бренды", max_length=250)
    slug = models.SlugField(verbose_name="URL", unique=True, db_index=True, max_length=255)


    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "Бренды"
        verbose_name_plural = 'Бренды'
Вернуться на верх