I can upload an image through the admin page, but the image can not be found when I navigate to the url that is generated by django. (404 error) The files are being uploaded to the folder:
我可以通过管理页面上传图像,但是当我导航到由django生成的网址时,无法找到图像。 (404错误)文件正在上传到文件夹:
project_root/media/eventbanner/1/
I have tried multiple solutions but none seem to work for my situation. Django 1.10 is being run local on Ubuntu 16.04.
我尝试了多种解决方案,但似乎没有一种方法适用于我的情况。 Django 1.10正在Ubuntu 16.04上运行。
The url I get is:
我得到的网址是:
http://localhost:8000/media/eventbanner/1/banner_image.jpg
Media root folder is located at:
媒体根文件夹位于:
/home/username/xxx/xxx/project_name/media
Code in HTML file:
HTML文件中的代码:
<div class="banner-image">
<img src="{{ event.eventbanner.banner_image.url }}"/>
</div>
url.py code:
url.py代码:
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from django.conf import settings
from django.conf.urls.static import static
app_name = 'events'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^details/(?P<event_id>[0-9]+)/$', views.details, name='details'),
url(r'^details/(?P<event_id>[0-9]+)/addcomment/$', views.add_comment, name='add_comment'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static'),]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
models.py
models.py
def validate_only_one_instance(obj):
model = obj.__class__
if (model.objects.count() > 0 and obj.id != model.objects.get().id):
raise ValidationError("Can only create 1 %s instance" % model.__name__)
class EventBanner(models.Model):
event = models.OneToOneField(Event, unique=True)
banner_image = models.ImageField(upload_to=get_image_path, blank=True, null=True)
def clean(self):
validate_only_one_instance(self)
13
The real problem here is that there is no relationship between this url http://localhost:8000/media/eventbanner/1/banner_image.jpg
and this location on disk /home/username/xxx/xxx/project_name/media
.
这里真正的问题是这个URL http:// localhost:8000 / media / eventbanner / 1 / banner_image.jpg与disk / home / username / xxx / xxx / project_name / media上的这个位置之间没有任何关系。
In a production application you'd have a web server where you'd store your Media
content, the serving URL would be MEDIA_ROOT
and you'd append ImageField.url
to this value to get a valid image path.
在生产应用程序中,您将拥有一个Web服务器,您可以在其中存储Media内容,服务URL将是MEDIA_ROOT,并且您将ImageField.url附加到此值以获取有效的图像路径。
What you need here is to set up a web server for your media images. At first that sounds like a lot of work, but Django provides a shortcut...
您需要的是为媒体图像设置Web服务器。起初这听起来像很多工作,但Django提供了一个捷径......
在开发中提供文件
You have some work you need to do to have the media files served locally. It requires some changes to your urls.py
...
您需要做一些工作才能在本地提供媒体文件。它需要对urls.py进行一些更改...
from django.conf import settings
from django.views.static import serve
# ... the rest of your URLconf goes here ...
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT,
}),
]
This uses the views.serve
bit and should only be used in DEBUG
mode. It overrides the path to media files(django's term for user uploaded comment like ImageField
). This will redirect those requests through the serve
view. Best I can tell this is a mini web server that will map those request routes to locations on disk and allow those locations to be reachable via HTTP urls.
这使用views.serve位,只应在DEBUG模式下使用。它会覆盖媒体文件的路径(django的用户上传注释的术语,如ImageField)。这将通过服务视图重定向这些请求。最好的我可以告诉这是一个迷你Web服务器,它将这些请求路由映射到磁盘上的位置,并允许通过HTTP URL访问这些位置。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/01/04/9b383efaffaeaf290295d7929fae3e.html。