python - Display image in template with Django -
i trying display images articles in blog project built django. here related code:
settings.py:
media_root = os.path.join(base_dir, 'media/') media_url = "/media/" urls.py (project):
urlpatterns += static(settings.media_url, document_root=settings.media_root) models.py:
class article(models.model): title = models.charfield(max_length=150) slug = models.slugfield(max_length=150) author = models.charfield(max_length=80) text = models.textfield(null=true) date = models.datetimefield(auto_now_add=true, auto_now=false, verbose_name="date de parution") image1 = models.imagefield() image2 = models.imagefield() image3 = models.imagefield() def __str__(self): return self.titre views.py:
def article(request, id, slug): article = get_object_or_404(article, id=id, slug=slug) return render(request, 'blog/post.html', {'article': article}) post.html:
<img src="{{ article.image1.url }}"/> <img src="{{ article.image2.url }}"/> <img src="{{ article.image3.url }}"/> however, not work. images not appear. suggestions ?
there shouldn't forward slashes before or after media, should .join(base_dir, 'media'). don't need add upload_to('media') because root media. not advised add images in media root, should create img folder inside media folder, , add upload_to('img/') in imagefield.
regarding inserting image preview inside textbox/textfield, yes possible, should separate question, although think there many identical questions on it.
Comments
Post a Comment