简体   繁体   English

Django显示用户上传的内容

[英]Django display user uploaded content

I have yet to wrap my head around django and URLs, and my confusion is now preventing me from doing what I feel like should be a very simple task. 我还没有对Django和URL有所了解,现在的困惑使我无法做自己想做的事情,这是非常简单的任务。

I have successfully implemented file upload. 我已经成功实现了文件上传。

In my settings.py file, I have added the specifications for where to store the uploaded files and the URL Django should use to serve them. 在我的settings.py文件中,我添加了关于上传文件存储位置的规范,以及Django应该用于提供这些文件的URL。

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

I also added the necessary line to urls.py to allow Django to serve files from MEDIA_URL. 我还向urls.py添加了必要的行,以允许Django从MEDIA_URL提供文件。

from django.conf.urls import url, include
from django.contrib import admin
from login_app import views as login_app_views
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    url(r'^admin/',     admin.site.urls),
    url(r'^login/',     login_app_views.login_user),

    # creating registered namespaces for each app
    url(r'^login/',     include('login_app.urls',       namespace = "login_app")),
    url(r'^CMIRS/',     include('dashboard_app.urls',   namespace = "dashboard_app")),
    url(r'^CMIRS/',     include('submit_app.urls',      namespace = "submit_app")),
    url(r'^CMIRS/',     include('filter_app.urls',      namespace = "filter_app")),
    url(r'^CMIRS/case/',include('report_app.urls',      namespace = "report_app")), 
    url(r'^CMIRS/',     include('search_app.urls',      namespace = "search_app")), 
    url(r'^search/',    include('haystack.urls')), ##used in navbar-search

 ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

In an app report_app, I want the webpage to display a hyperlink that can be used to view an uploaded file. 我希望在应用程序report_app中,网页显示可用于查看上传文件的超链接。 When I click on the hyperlink, I want it to request the URL to the uploaded file. 当我单击超链接时,我希望它请求上传文件的URL。

The upload looks like such in my models: 在我的模型中,上传看起来像这样:

upload1 = models.FileField(upload_to = 'documents/%Y/%m/%d/')

I am having trouble figuring out what to use in the render(request) in my view and how to correctly code this in HTML. 我在弄清楚在视图中要在render(request)中使用什么以及如何正确地用HTML编写代码时遇到了麻烦。 When I attempt to use "media", I get an error saying it cannot be matched. 当我尝试使用“媒体”时,出现错误消息说无法匹配。

Here is a snippet of the HTML I am trying: 这是我正在尝试的HTML的片段:

<dt>Upload</dt><dd><tr><td><a href="{% url 'media' case.pk %}">{{ case.upload1 }}</a></td></tr></dd>

I am also confused as how to set up my render(request) so that it knows to access media/, and then go to the correct documents/Y/M/D depending on the primary key. 我也很困惑如何设置我的render(request),以便它知道要访问media /,然后根据主键转到正确的document / Y / M / D。

You don't want to use the url tag here at all. 您根本不想在这里使用url标记。 Your media's URL is stored in your model, and has nothing to do with Django's path resolution logic. 媒体的URL存储在模型中,与Django的路径解析逻辑无关。 Just reference the url method of the field: 只需引用该字段的url方法:

<a href="{{ case.upload1.url }}">

See the docs . 请参阅文档

(Note also that serving files via your urls.py like this works in dev only; for prod you'll need to configure your webserver to do it.) (另请注意,像这样通过urls.py提供文件仅在开发人员中起作用;对于产品,您需要配置Web服务器来执行此操作。)

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

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