Answer the question
In order to leave comments, you need to log in
Image upload via Django form?
Worked with the documentation, but did not solve the problem.
You need to send a picture models to the blog post
from django.db import models
from django.utils import timezone
class Post(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
image = models.ImageField(null=True, blank=True, upload_to='blog/images/')
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.published_date = timezone.now()
post.save()
return redirect('/', pk=post.pk)
else:
form = PostForm(request.POST, request.FILES)
return render(request, 'blog/post_edit.html', {'form': form})
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<h1>Новий пост</h1>
<div class="col-md-12 col-lg-8 main-content">
<form method="POST">{% csrf_token %}
<div class="row">
<div class="col-md-4 form-group">
{% render_field form.title type="text" id="name" class="form-control " title="Заголовок" %}
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
{% render_field form.text name="message" id="message" class="form-control " %}
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
{% render_field form.image %}
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<input type="submit" value="Опубликовать" class="btn btn-primary">
</div>
</div>
</form>
</div>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question