我已多次浏览Django文档和多个stackoverflow问题,但我无法让媒体服务工作。获取文件时没有错误,只有错误。
帮助我了解如何投放媒体文件的任何帮助或其他来源。
Settings.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='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
带图像区域的模型: class Product(models.Model):
product_category = models.CharField(max_length=20)
product_id = models.AutoField(primary_key=True)
product_name = models.CharField(max_length=50)
product_description = models.CharField(max_length=100)
small_price = models.IntegerField()
medium_price = models.IntegerField()
large_price = models.IntegerField()
rating = models.FloatField()
image = models.ImageField( default='static/imgs/products/Cleanser.png')
调用媒体的模板代码:
{% 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%}
编辑:
媒体服务的URLs.py添加:
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)
答案 0 :(得分:0)
我还必须修改我的主urls.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