django 1.8中的ImageField类不在模板中显示图像

时间:2015-06-05 12:29:13

标签: python django

根据代码,图片应显示在index.html模板上,但img src

似乎存在问题

我的models.py

from django.db import models


class Rockinfo(models.Model):
   rock_name = models.CharField(max_length=200,default="ac/dc")
   rock_img = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
   rank = models.IntegerField(default=0)

   def __str__(self):              
       return self.rock_name

我的admin.py

from django.contrib import admin
from .models import Rockinfo, Rockvids

class ChoiceInline(admin.TabularInline):
  model = Rockvids
  extra = 10

class RockinfoAdmin(admin.ModelAdmin):
  fieldsets = [
    ('The Fucking Band',               {'fields': ['rock_name']}),
    ('Image', {'fields': ['rock_img']}),
  ]
  inlines = [ChoiceInline]  
  list_display = ('rock_name', 'rock_img')
  list_filter = ['rank']
  search_fields = ['rock_name']


admin.site.register(Rockinfo, RockinfoAdmin)

我的申请urls.py

 from django.conf.urls import url

 from . import views

 urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
 ]

我的项目models.py

 from django.conf.urls import include, url
 from django.contrib import admin

  urlpatterns = [
     url(r'^rockinglist/', include('rockinglist.urls',  namespace="rockinglist")),
     url(r'^admin/', include(admin.site.urls)),
     ]

我的views.py文件

   from django.shortcuts import get_object_or_404, render
   from django.http import HttpResponseRedirect, HttpResponse
   from django.core.urlresolvers import reverse
   from django.views import generic
   from .models import Rockvids, Rockinfo


   class IndexView(generic.ListView):
   template_name = 'rockinglist/index.html'
   context_object_name = 'latest_rockinfo_list'

   def get_queryset(self):
       return Rockinfo.objects.order_by('-rank')[:5]

index.html文件

   {% if latest_rockinfo_list %}
   <ul>
   {% for rockinfo in latest_rockinfo_list %}
   <h1>{{ rockinfo.rock_name }}</a></li>
   <img src="img\{{ rockinfo.rock_img }}" alt="ac/dc">
   {% endfor %}
   </ul>
   {% else %}
   <p>No</p>
   {% endif %}

我认为问题出在图像的根部。某些django无法访问图像。我想显示rock_name以及相关的rock_img。有人可以帮我解决这个问题。谢谢你。

2 个答案:

答案 0 :(得分:5)

在你的项目的urls.py中,添加:

# your other imports here ...
# ...
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLs ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在您的settings.py文件中,请确保您拥有:

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

在模板中:

<img src="{{ rockinfo.rock_img.url }}" alt="ac/dc">

注意:如果您的设置中未定义BASE_DIR,请将其放在设置文件的顶部:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

答案 1 :(得分:2)

首先检查您的设置中是否提供了MEDIA_URL。 Django在开发过程中没有服务器媒体,因此您必须在项目urls.py中拥有以下内容:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

之后,您可以使用

访问模板中的图像
{{ rockinfo.rock_img.url }}