Python 在 Django 模板中显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15175032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:32:00  来源:igfitidea点击:

show images in Django templates

pythondjangoimagetemplates

提问by hln

Can someone help me with this issue: I have a Django porject,

有人能帮我解决这个问题吗:我有一个 Django 项目,

in settings.py

在设置.py

 MEDIA_ROOT = 'C:/Users/hl/workspace/beer/media'
 MEDIA_URL = '/media/'
 STATICFILES_DIRS = (
    'C:/Users/hl/workspace/beer/media'
 )

and in models.py

并在models.py中

image1= models.ImageField(upload_to=settings.MEDIA_ROOT)

and in url.py

并在 url.py

 (r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),

in views

在视图中

def allBeer(request): 
      beers=Beer.objects.all().order_by("name")
      context={'beers': beers}
      return render_to_response('AllBeers.html',context,context_instance=RequestContext(request))

and in html

并在 html

 {%for beer in beers %}
    <p>
        <a href="/beers/{{beer.slug}}/">
            <img scr="{{beer.image1.url}}">{{beer}}
        </a>
    </p>
 {% endfor%}

It has not problem to load images, but images wont show in html file. I have searched and read a lot from internet but I still couldn't figure out.

加载图像没有问题,但图像不会显示在 html 文件中。我已经从互联网上搜索并阅读了很多内容,但我仍然无法弄清楚。

Can anyone tell me why?

谁能告诉我为什么?

采纳答案by catherine

image1= models.ImageField(upload_to=images)


from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from project_name import settings

admin.autodiscover()
urlpatterns = patterns('',
    ...........
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()


<img src="{{MEDIA_URL}}{{beer.image1}}">

settings.py

设置.py

import os

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT


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


STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'


STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    os.path.join(SITE_ROOT, 'staticfiles'),
)

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(SITE_ROOT, 'templates'),
)

回答by Aidan Ewen

You're messing with the srcattribute of the image. It should just be -

你搞乱src了图像的属性。应该只是——

<img src="{{beer.image1.url}}" /> <!-- from the media url -->

Don't add anything to the end - django know's the url to serve the image from - that's what the ImageField on the model does.

不要在最后添加任何内容 - django 知道提供图像的 url - 这就是模型上的 ImageField 所做的。

I don't know that there's actually anything wrong with your url conf, but the pattern recommended in the docsis -

我不知道你的 url conf 实际上有什么问题,但文档中推荐的模式是 -

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

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

回答by Ansif_Muhammed

the location of your images are important. it has to be in the static folder and that has to be defined in the settings.

图片的位置很重要。它必须在静态文件夹中,并且必须在设置中定义。