gpt4 book ai didi

Django 1.9 - 不显示用户上传的图像

转载 作者:行者123 更新时间:2023-12-03 01:44:34 24 4
gpt4 key购买 nike

我正在尝试显示用户已上传的图像,但无论我如何尝试,我都会收到损坏的链接图标。我已经在 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"这给了我一个 url http://127.0.0.1:8000/media/media_root/pics/originals/DSC_1376.jpg 。这对我来说似乎是正确的文件位置,但它仍然没有显示。

就像我说的,在我看来,一切都是按照 Django 文档和我读过的所有其他问题中描述的方式设置的。我错过了什么?

提前谢谢您。

编辑

我需要修改我的 STATICFILES_DIRS上传媒体的设置?这是我现在拥有的:

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_files"),
)

这是我放置所有 CSS 和 javascript 文件的地方。

最佳答案

您忘记连接 MEDIA_URL{{ photo.url }}

尝试:

<img src="{% get_media_prefix %}{{ photo.url }}" alt="">

更多关于{% get_media_prefix %} HERE在文档中。

关于Django 1.9 - 不显示用户上传的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com