Почему не работает форма django?
Вот код views.py:
def create_trailer(request):
form = TrailerForm()
if request.method == 'POST':
if form.is_valid():
form = TrailerForm(request.POST, request.FILES)
form.save()
return render(request, 'polls/create_trailer.html', {'form': form})
Вот код create_trailer.html:
<div class="create_trailer">
<form class="create_trailer_form" method="post">
{% if form.is_valid %}
<h1>Форма валидна</h1>
{% else %}
{{ form.errors }}
{% endif %}
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Создать статью">
</form>
</div>
Вот код models.py:
class Trailer(models.Model):
name_trailer = models.CharField("Название трейлера", max_length=200)
pub_date = models.DateTimeField('Дата публикации трейлера', default=timezone.now)
preview_trailer = models.ImageField("Превью трейлера", upload_to='photo', default=None)
trailer = models.FileField("Трейлер", upload_to='video')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default="anonim")
Вот код forms.py:
class TrailerForm(ModelForm):
class Meta:
model = Trailer
fields = ['name_trailer', 'preview_trailer', 'trailer', 'author']