简体   繁体   English

Django - 为用户上传的图像提供服务

[英]Django - serving user uploaded images

I am having some problems with serving user uploaded files from my Django application: 我在从Django应用程序提供用户上传文件时遇到一些问题:

from models.py: 来自models.py:

class Picture (models.Model):

    title = models.CharField(max_length=48)

    date_added = models.DateTimeField(auto_now=True)

    content = models.ImageField(upload_to='pictures')

From the Django admin the files get uploaded to the user_res/pictures/ folder. 从Django管理员,文件上传到user_res / pictures /文件夹。

from the project's settings.py: 从项目的settings.py:

MEDIA_ROOT = 'user_res'

MEDIA_URL = '/user_res/'

STATIC_ROOT = ''

STATIC_URL = '/static/'

Every time I try to reference a static resource (namely css or js files), everything works fine using URLs such as 每次我尝试引用静态资源(即css或js文件)时,一切都可以正常使用诸如

http://localhost:8000/static/<subfolder>/main.css.

However, I cannot access user uploaded files (which get created by the admin interface in the user_res/pictures folder with a relative URL such as 但是,我无法访问用户上传的文件(由user_res / pictures文件夹中的管理界面创建的文件与相对URL,如

user_res/pictures/test.jpg

the URL is dynamically created with this line of code from a Django Picture model callable: 使用来自Django Picture模型可调用的此代码行动态创建URL:

return '<img src="{}"/>'.format(self.content.url)

I have no dedicated url-s for either static or media files in the url.py file. 我没有url.py文件中的静态或媒体文件的专用url-s。

Does anybody have any idea as to how to make Django serve the media files? 有没有人知道如何让Django提供媒体文件? I understand that for live environments I will need to configure an http server to serve that particular directory, but for now I want to maintain a lightweight development suite. 我知道对于实时环境,我需要配置一个http服务器来服务该特定目录,但是现在我想维护一个轻量级的开发套件。

Thank you. 谢谢。

Edit your urls.py file as shown below. 编辑urls.py文件,如下所示。

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)

edit your projects settings.py to look like: 编辑您的项目settings.py看起来像:

#Rest of the settings
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
STATIC_ROOT = ''
STATIC_URL = '/static/'

Please read the official Django documentation about serving files uploaded by a user carefully. 请仔细阅读有关提供用户上传文件的官方Django文档。 Link to docs: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user 链接到文档: https//docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user

I think the url attribute returns a relative URL ( Django's FileField documentation ), so you should have: 我认为url属性返回一个相对URL( Django的FileField文档 ),所以你应该:

return '<img src="{}"/>'.format(MEDIA_URL + self.content.url)

Relative URLs won't work, as a user visiting "http://localhost/books/" would be requesting "http://localhost/books/user_res/pictures/test.jpg". 相对URL不起作用,因为访问“http:// localhost / books /”的用户将请求“http://localhost/books/user_res/pictures/test.jpg”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM