Django 1.9:图像未在模板中呈现

时间:2016-07-15 02:47:02

标签: python django image

我是django新手,我正在写一个网站,并试图上传和图像到关于页面。当我右键单击并在渲染页面上查看源时,图像网址似乎很好。但图像没有出现

以下是我的代码段

urls.py
-------
urlpatterns = [url(r'^$', views.VBlogIndex.as_view(), name="index"),
url(r'^about/$', views.aboutpage, {'url': '/about/'}, name="about_
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py

from django.contrib.flatpages.models import FlatPage as FlatPageOrig

class AboutPage(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField(blank=True)
    image = models.ImageField(upload_to="about")

    def __str__(self):
        return self.image.url

about.html

    <div class="separator" style="clear: both; text-align: center;">
      <img src="{{ aboutpage.image.url }}"/>
    </div>
    <div>{{ aboutpage.content|markdown }}</div>

views.py

from . import models
def aboutpage(request, url):
    if not url.startswith('/'):
        url = '/' + url
    site_id = get_current_site(request).id
    try:
        f = get_object_or_404(models.AboutPage,
            url=url, sites=site_id)
    except Http404:
        raise
    template = loader.get_template("static/about.html")
    response = HttpResponse(template.render({'aboutpage': f}, request))
    return response

settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, "static/images")

MEDIA_URL = '/static/images/'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, "templates"),],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.request',
            'django.template.context_processors.media',
        ],
    },
},
]

页面渲染后,图像不会显示,但以下内容显示在渲染页面的html源代码中

django命令行说&#34; GET /static/images/about/Dashbox_Asset_Image.jpg HTTP / 1.1&#34; 404 1822

0 个答案:

没有答案