Answer the question
In order to leave comments, you need to log in
Why is django form not working?
Why is django form not working?
Here is the views.py code:
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})
<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>
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")
class TrailerForm(ModelForm):
class Meta:
model = Trailer
fields = ['name_trailer', 'preview_trailer', 'trailer', 'author']
Answer the question
In order to leave comments, you need to log in
There was a problem in views.py
So it was necessary:
def create_trailer(request):
form = TrailerForm(request.POST, request.FILES)
if request.method == 'POST':
if form.is_valid():
form.save()
return render(request, 'polls/create_trailer.html', {'form': form})
Try like this:
<form enctype="multipart/form-data" method="post" action="/foo/">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question