简体   繁体   English

Django:为用户上传的文件提供服务

[英]Django: serving user uploaded files

I'm new to Django and Python.I want to display a link to my files stored in my static folder.Here is the required code from settings.py : 我是Django和Python的新手,我想显示存储在我的static文件夹中的文件的链接,这是settings.py所需的代码:

MEDIA_ROOT = os.path.join(BASE_DIR,'static') MEDIA_URL = "/media/"

When I access these files through my admin I get the following url displayed in my browser: 通过admin访问这些文件时,在浏览器中显示以下网址:

http://127.0.0.1:8000/media/filename.pdf http://127.0.0.1:8000/media/filename.pdf

Hence what I tried is to give a link to the file in my HTML code : 因此,我尝试在HTML代码中提供指向该文件的链接:

<a href="/{{instance.docFile.url}}/">File</a>

But instead of displaying http://127.0.0.1:8000/media/filename.pdf it just displays /media/filename.pdf which results in an error. 但是,除了显示http://127.0.0.1:8000/media/filename.pdf它仅显示/media/filename.pdf会导致错误。

Adding localhost:8000/ before {{instance.docFile.url}} also didn't work. {{instance.docFile.url}}之前添加localhost:8000/也不起作用。

Why isn't this working?What is the correct way to display link to my files? 为什么这样不起作用显示链接到我的文件的正确方法是什么?

Let me know if anything else is required.Thank you. 让我知道是否还有其他需要。谢谢。

The error is coming because your file is not stored in media .it is stored in static but your database store media URL. 由于您的文件未存储在media因此出现了错误。它存储在static但数据库存储了media URL。 change your MEDIA_ROOT (given below) and try to upload one more time 更改您的MEDIA_ROOT(如下所示)并尝试再上传一次

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Don't put slash : 不要放斜线:

<a href="{{instance.docFile.url}}">File</a>

in your urls.py 在您的urls.py中

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

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

in your settings.py 在您的settings.py中

MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/uploads/'

Then in your template 然后在您的模板中

<a href="{{instance.docFile.url}}">File</a>

If you want know refer the docs Manage static files 如果您想知道,请参阅文档“ 管理静态文件”

Please see the Django setting MEDIA_ROOT documentation to better understand what your trying to do. 请参阅Django 设置MEDIA_ROOT文档,以更好地了解您的操作。

First MEDIA_ROOT and STATIC_ROOT must have different values. 首先,MEDIA_ROOT和STATIC_ROOT必须具有不同的值。

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

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

See static files documentation for more details. 有关更多详细信息,请参见静态文件文档。

Second, You need to add these settings to your base urls.py. 其次,您需要将这些设置添加到基本urls.py中。

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

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

If you want to use {{ MEDIA_URL }} in your templates, add 'django.template.context_processors.media' in the 'context_processors' option of TEMPLATES in settings.py. 如果要在模板中使用{{ MEDIA_URL }} ,请在settings.py中的TEMPLATES“ context_processors”选项中添加“ django.template.context_processors.media”

At the top of your HTML page add {% load static %} then to display your media: HTML页面的顶部添加{%load static%},然后显示您的媒体:

<img src="{{ MEDIA_URL }}{{ docFile }}" alt="test">

Please take your time with the Django Documentation, trust me it will help and also checkout the File upload settings 请花些时间阅读Django文档,相信我会有所帮助,并请查看文件上传设置

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

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