Django不显示图像字段

时间:2020-10-21 12:28:35

标签: django django-models django-forms django-views

上传时未显示图像,这是什么问题?

setting.py

MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = 'media/'

html。

    {% for tt in article %}
<div class="content_box">
    <div class="content_l left">
        <div class="horoscope_box_big">
            <div class="blog_img1">
                <img src="{{ tt.image.url }}" alt="{{tt.title}}" width="50%" /> {{ tt.image.url }}
            </div>
            <h1 class="blog_title1"> {{ tt.Title }}
<div class="blog_descr1"><h1>more about</h1>

<p> {{ tt.body }}</p>

</div>
        </div>
    
    
{% endfor %}

models.py

class Blogmodel(models.Model):
    Title = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to="images/",null=True)
    body = RichTextField(blank=True, null=True)
    slug = AutoSlugField(populate_from='Title', unique=True,blank=True, null=True)

    def __str__(self):
        return self.Title

views.py

def blog_list(request):
    articles = Blogmodel.objects.all()
    args = {'articles':articles}
    return render(request,'blog.html',args)

def blogDetail(request, slug):
    article = Blogmodel.objects.filter(slug=slug)
    args = {'article':article}
    return render(request,'blogdetail.html',args)

urls.py

from django.urls import path
from .views import singCategory,Home,blog_list,blogDetail
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('',Home,name= "Home"),
    path('horoscope/<slug:slug>/<slug:cat>',singCategory,name='singCategory'),

    path("blog/",blog_list, name="blog_list"),
    path("blog/<slug:slug>",blogDetail, name="blogDetail"),  
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

2 个答案:

答案 0 :(得分:0)

您的 settings.py

MEDIA_URL = '/media/'

您的模板:

<a href="/blog/{{ article.slug }}" class="blog_img"><img src="{{ article.image.url }}"></a>

答案 1 :(得分:0)

检查是否已安装枕头,点子安装枕头