Python Django:服务媒体文件时遇到问题
我已经阅读了Django文档多次,并提出了多个stackoverflow问题,但我根本无法让媒体服务发挥作用。没有错误,只是在获取文件时出错 任何帮助或额外的来源,以帮助我了解如何服务媒体文件 设置.pyPython Django:服务媒体文件时遇到问题,python,django,Python,Django,我已经阅读了Django文档多次,并提出了多个stackoverflow问题,但我根本无法让媒体服务发挥作用。没有错误,只是在获取文件时出错 任何帮助或额外的来源,以帮助我了解如何服务媒体文件 设置.py STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') MEDIA_ROOT=BASE_DIR + os.path.join(BASE_DIR, 'store/media/') MEDIA_URL
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT=BASE_DIR + os.path.join(BASE_DIR, 'store/media/')
MEDIA_URL='media/'
我有一个常用的文件夹结构:
/store { static, media, migrations,templates, etc }
/venv
/staticfiles
manage.py
Procfiles
README
requirements.txt
runtime.txt
我的要求.txt
appdirs==1.4.3
Django==1.10.5
gunicorn==19.6.0
olefile==0.44
packaging==16.8
Pillow==4.0.0
psycopg2==2.7
pyparsing==2.2.0
six==1.10.0
virtualenv==15.1.0
whitenoise==3.3.0
带有图像字段的模型:
类别产品(型号.型号):
模板代码调用媒体:
{% for product in all_products %}
<div class="item col-xl-4 col-lg-4" style="max-width: 400px;border-color:black;" >
<div class="thumbnail" style="box-shadow: 1px 1px 10px grey;">
{% load staticfiles %}
<img class="group list-group-image" style="height:200px;width:120px;" src="{{ product.image }}">
<div class="caption" style="text-align:Center;">
<p class="group inner list-group-item-heading" style="font-family:Raleway;font-size:23px;color:black;">
<strong>{{ product.product_name}}</strong></p>
<p class="group inner list-group-item-text" style="font-family:Roboto,sans-serif;font-style:italic;color:#222;opacity: 0.5">
{{ product.product_description }}</p>
<div class="row">
<div class="col-xs-12 ">
<div class="form-group" >
<label for="size">Sizes</label>
<select class="form-control" class="size" style="max-width: 50%;margin:auto;">
{% if product.product_category == "Milkshakes" %}
<option value="{{product.medium_price}}" > Medium</option>
{% else %}
<option value="{{product.small_price}}" > Small ( 285ml )</option>
<option value="{{product.medium_price}}" >Medium ( 350ml ) </option>
<option value="{{product.large_price}}">Large ( 500ml )</option>
{%endif%}
</select>
<input type="hidden" value="{{product.product_id}}" class='hidden'>
<p style="margin-top:15px;"> <strong>Quantity </strong> </p>
<button type="button" class="btn btn-default btn-circle less" >-</button>
<input type="button" class="btn btn-default btn-circle quantity" value=1></input>
<button type="button" class="btn btn-default btn-circle more" >+</button>
<p> </p>
{% if discount != 0 %}
<p style="font-family: Raleway;color:black;font-size: 20px"> Discount <span style="color:rebeccapurple;opacity: 0.5">{{ discount }}%</span></p>
{% endif %}
<p class="sizer" style="color:black" >MUR </p> <p class="price" style="color:rebeccapurple;opacity: 0.8">{{ product.small_price }}</p>
<p> </p>
<button type="input" class="btn btn-success buy" style="border-
color:#8A2BE2;background-color: #8A2BE2;margin-top:-20px;font-
size:17px;font-family: Raleway"> Add <i class="fa fa-shopping-cart"
aria-hidden="true"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor%}
我还必须修改我的主URL.py以提供媒体文件:
urlpatterns = [
url(r'^homepage/', include('homepage.urls')),
url(r'^login/', views.login, {'template_name': 'login.html'}, name='login'),
url(r'^register/', include('register.urls')),
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # For serving media files on development server
在
MEDIA\u ROOT=BASE\u DIR+os.path.join(BASE\u DIR,'store/MEDIA/')之后放置一个print(MEDIA\u ROOT)
。你得到了什么?可能重复的@nik_m它实际上打印空白(heroku部署)MEDIA_ROOT
应该与STATIC_ROOT
几乎相同,除非你有一些独特的案例。为什么要将BASE\u DIR
添加到os.path.join(BASE\u DIR,'store/media/')
?尝试执行您为STATIC\u ROOT:MEDIA\u ROOT=os.path.join(BASE\u DIR,'store/MEDIA/')所做的操作。
您无法在Heroku系统中存储用户上载的媒体文件。您必须使用像S3这样的外部存储器。
from django.contrib.staticfiles.urls import static,staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns() urlpatterns += staticfiles_urlpatterns()
-urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT) +urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns = [
url(r'^homepage/', include('homepage.urls')),
url(r'^login/', views.login, {'template_name': 'login.html'}, name='login'),
url(r'^register/', include('register.urls')),
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # For serving media files on development server