[英]Django: cannot render uploaded image in template
I've created a model that accepts any kind of file using FileField()
in my model.我创建了一个模型,该模型在我的模型中使用
FileField()
接受任何类型的文件。
I've uploaded correctly some files and saved the reference in the db, as I can see in the admin interface, and in the folder "media".我已经正确上传了一些文件并将参考保存在数据库中,正如我在管理界面和文件夹“media”中看到的那样。
However, cannot render this images in the template.但是,无法在模板中渲染此图像。 I need to render it so my users will be able to download them to their computer.
我需要渲染它,以便我的用户能够将它们下载到他们的计算机上。
These are the paths to my images:这些是我的图像的路径:
media/imagenes/468x60.jpg #relative path
/home/ogonzales/Escritorio/web_proyects/gallito/media/imagenes/468x60.jpg #full path
Project structure项目结构
gallito (Django project folder)
|_gallito
|_main_app
|_static
|_templates
|_main_app
|_pedidos.html
|_media
|_imagenes
|_468x60.jpg
|_728x90.jpg
|_templates
|_registration
|_login.html
|_base.html
models.py :模型.py :
class TamaniosCantidades(models.Model):
TAMANIOS = (('2x2', '2" x 2"',), ('3x3', '3" x 3"',),
('4x4', '4" x 4"',), ('5x5', '5" x 5"',))
CANTIDADES = (('50', '50',), ('100', '100',),
('150', '150',))
tamanios = models.CharField(max_length=10, choices=TAMANIOS)
cantidades = models.CharField(max_length=10, choices=CANTIDADES)
imagenes = models.FileField(upload_to='imagenes/')
uploaded_at = models.DateTimeField(auto_now_add=True)
forms.py:表格.py:
class TamaniosCantidadesForm(forms.ModelForm):
tamanios = forms.ChoiceField(choices=TAMANIOS, widget=forms.RadioSelect(), label='Selecciona un tamaño')
cantidades = forms.ChoiceField(choices=CANTIDADES, widget=forms.RadioSelect(), label='Selecciona la cantidad')
class Meta:
model = TamaniosCantidades
# fields = ['tamanios', 'cantidades',]
fields = ['tamanios', 'cantidades', 'imagenes']
views.py:视图.py:
def pedidos(request):
pedidos = TamaniosCantidades.objects.all()
return render(request, 'main_app/pedidos.html', {'pedidos': pedidos})
urls.py:网址.py:
urlpatterns = [
path('', views.index),
path('productos/', views.productos),
path('productos/die-cut-stickers', views.die_cut, name='die-cut-stickers'),
path('post_url/', views.post_treasure, name='post_treasure'),
path('post_url_tamanioscantidades/', views.post_tamanioscantidades, name='post_tamanioscantidades'),
path('pedidos/', views.pedidos),
# path('login/', views.login_view, name='login'),
# path('logout/', views.logout, {'next_page': settings.LOGOUT_REDIRECT_URL}, name='logout'),
]
HTML: HTML:
<div>
<p>Mi Imagen</p>
<img src="/home/ogonzales/Escritorio/web_proyects/gallito/media/imagenes/468x60.jpg">
{% for pedido in pedidos %}
<p> {{ pedido.imagenes }}</p>
<img src="{{ pedido.imagenes.url }}" alt="Smiley">
{% endfor %}
</div>
UPDATE 1:更新1:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
UPDATE 2:更新 2:
this is the rendered html这是呈现的 html
<p> imagenes/728x90.jpg</p>
<img src="/media/imagenes/728x90.jpg" alt="Smiley">
<p> imagenes/468x60.jpg</p>
<img src="/media/imagenes/468x60.jpg" alt="Smiley">
Try adding the following snippet in your URLs.py
if you haven't done so:如果您还没有这样做,请尝试在您的
URLs.py
添加以下代码段:
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT).
This should enable your media to work within your browser while in development.这应该使您的媒体能够在开发过程中在浏览器中工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.