我正在尝试显示用户上传的图片,但无论我尝试什么,我都会收到损坏的链接图标。我已经搜索并搜索了文档,在SO和其他地方几天,现在无济于事。我是Django的新手并且目前正在开发中,所以我确定我已经犯了一些其他的新手错误,但是现在我唯一关心的是在模板中显示上传的图像。
以下是我的代码的相关摘要:
settings.py
MEDIA_URL = '/media/media_root/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media", "media_root")
urls.py
urlpatterns = [
url(r'^admin/?', admin.site.urls),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^about/?', profiles.views.about, name='about'),
url(r'^properties/single/?', properties.views.single, name='single_mens'),
url(r'^properties/married/?', properties.views.married, name='married'),
url(r'^properties/add/add_photos/?', properties.views.add_photos, name='add_photos'),
url(r'^properties/add/?', properties.views.add_rental, name='add_rental'),
url(r'^', profiles.views.home, name='home'),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
models.py
class RentalPicModel(models.Model):
def __unicode__(self):
return self.image.url
image = models.ImageField(upload_to="pics/originals/", null=True)
rental = models.ForeignKey(RentalModel, on_delete=models.CASCADE)
forms.py
class AddPhotosForm(forms.ModelForm):
class Meta:
model = RentalPicModel
fields = ['image', 'rental']
def clean_image(self):
return self.cleaned_data['image']
def clean_rental(self):
return self.cleaned_data['rental']
views.py
def add_photos(request):
form = AddPhotosForm
current_rental = None
current_photos = []
if request.method == "POST":
form = AddPhotosForm(request.POST, request.FILES)
if request.POST.get('another'):
if form.is_valid():
cleaned_image = form.cleaned_data['image']
cleaned_rental = form.cleaned_data['rental']
current_rental = cleaned_rental
pic = RentalPicModel(image=cleaned_image, rental=cleaned_rental)
pic.save()
current_photos = RentalPicModel.objects.filter(rental=current_rental)
current_photos = [rental.image for rental in current_photos]
for photo in current_photos:
print photo
context = {
'form' : form,
'photos' : current_photos,
}
return render(request, "add_photos.html", context)
此处print
语句(上传一张照片后)的输出为:pics/originals/DSC_1376.jpg
,我可以看到该文件已保存到该位置。
add_photos.html
<div class="container">
<h1>Upload your photos here.</h1>
<br>
<div class='row'>
<form method="POST" action="" enctype="multipart/form-data"> {% csrf_token %}
{{ form|crispy }}
<div class='col col-xs-3'></div>
<div class='col col-xs-3'>
<input class="btn btn-block btn-info" name="another" type="submit" value="Save and Add Another">
</div>
<div class='col col-xs-3'>
<input class="btn btn-block btn-primary" name="finish" type="submit" value="Save and Finish">
</div>
<div class="col col-xs-3"></div>
</form>
</div>
{% if photos|length > 0 %}
<h2>Uploaded photos:</h2>
{% for photo in photos %}
<div class='row'>
<img src="{{ photo.url }}" alt="">
</div>
{% endfor %}
{% endif %}
</div>
当我检查<img>
元素时,我看到src="/media/media_root/pics/originals/DSC_1376.jpg"
给了我一个http://127.0.0.1:8000/media/media_root/pics/originals/DSC_1376.jpg
的网址。这对我来说似乎是正确的文件位置,但它仍然没有显示。
就像我说的那样,在我看来,在Django文档和我在SO上阅读的所有其他问题中,如何描述它是如何描述的。我错过了什么?
提前谢谢。
修改
我是否需要为上传的媒体修改我的STATICFILES_DIRS
设置?这就是我现在所拥有的:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_files"),
)
这是我放置所有CSS和javascript文件的地方。
答案 0 :(得分:2)
您忘了连接MEDIA_URL
和{{ photo.url }}
。
<强>尝试:
<img src="{% get_media_prefix %}{{ photo.url }}" alt="">
有关文档中{% get_media_prefix %}
HERE的更多信息。
答案 1 :(得分:2)
我的网址是问题所在。当它尝试检索媒体文件时,它在匹配任何媒体网址之前匹配url(r'^', profiles.views.home, name='home')
。一个简单的$
修复了它:
url(r'^$', profiles.views.home, name='home')