我一直在Django开发自己的网站,并遇到了一个问题,
我有一个模特:
class Text_Image_Page(TrainingModulePage):
page_num = models.IntegerField(default = 0)
title = models.CharField(max_length=200)
text = models.CharField(max_length=200)
image_desc = models.CharField(max_length=200)
image = models.ImageField(upload_to=settings.MEDIA_ROOT, default = 'null')
training_module = models.ForeignKey(TrainingModule, related_name='text_image_pages', null=True)
def __str__(self):
return "Image Page"
我正在使用modelForm来创建这个模型,一切正常,直到我尝试在模板中显示模型。
我的settings.py中的我的媒体根
STATIC_URL = '/static/'
MEDIA_URL = '/superchem_media/'
STATIC_ROOT = '/home/anthonycalab/webapps/superchem_static/'
MEDIA_ROOT = '/home/anthonycalab/webapps/superchem_media/'
图像正在上传到媒体根目录,因为我可以看到它们通过filezilla存在但是当我在模板中显示我的图像时:
<img src={{t.image.url}} alt="{{t.image_desc}}" style="width:304px;height:228px;">
它不会显示图像,如果我加载图像直接链接,我会收到404错误,并显示以下消息:
Request Method: GET
Request URL: http://anthonycalab.webfactional.com/home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg
Using the URLconf defined in superchem.urls, Django tried these URL patterns, in this order:
^training_modules/
^admin/
^accounts/
^superchem\_media\/(?P<path>.*)$
^static\/(?P<path>.*)$
The current URL, home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg, didn't match any of these.
任何想法都是我错误的做法?
编辑:
问题已缩小为:
{{t.image.url}} = /home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg
我想要的时候
/媒体/ [ImageLink的] .JPG
答案 0 :(得分:0)
在主urls.py文件中添加媒体网址
from django.contrib import admin
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
admin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('home.urls')),
]
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': False}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)
urlpatterns += staticfiles_urlpatterns()
答案 1 :(得分:0)
settings.py
import os
def root(x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x)
MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
root('static'),
)
TEMPLATE_CONTEXT_PROCESSORS = (
'-------------------'
'django.core.context_processors.media',
'django.core.context_processors.static',
'-----------'
)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': False}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)
urlpatterns += staticfiles_urlpatterns()