通过管理页面上传图像时,我目前遇到问题,该图像未显示在模板上。当我检查页面源代码时,似乎无法识别在哪里寻找上载的图像,因为images标签中的src是空白的。
models.py
from django.db import models
from django.utils import timezone
class post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
image = models.ImageField(upload_to='blog_pics', null=True, blank=True)
Keywords = models.CharField(max_length=100, default= "")
def __str__(self):
return self.title
blogs.html
{% extends "blog/base.html" %}
{% block content %}
{% for post in posts %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<h2><a class="article-title">{{ post.title }}</a></h2>
{% if post.image %}
<img class="rounded-square blog-img" src="{{ blog_pics.image.url }}", align="top">
{% endif %}
<p class="article-content">{{ post.content }}</p>
<small class="text-muted">{{ post.date_posted }}</small>
</div>
</div>
</article>
{% endfor %}
{% endblock content %}
urls.py
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index, name='blog-index'),
path('about/', views.about, name='blog-about'),
path('blogs/', views.blogs, name='blog-blogs'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)