[英]Cannot Get image From Database Django Framework
I want to create a setting in admin panel which will help me to change the website logo from the admin panel.我想在管理面板中创建一个设置,这将帮助我从管理面板更改网站徽标。 I create a model for database and it's working and uploading an image to the database + folder.
我为数据库创建了一个模型,它正在工作并将图像上传到数据库 + 文件夹。 but I cannot get this image to the Website template.
但我无法将此图像发送到网站模板。 when I'm viewing page source image SCR is empty.
当我查看页面源图像时,SCR 为空。
my Model.py我的模型.py
class WebLogo(models.Model):
Logotitle = models.CharField(max_length=50,unique=False)
SiteLogo = models.ImageField(upload_to='webimages')
def __str__(self):
return self.Logotitle
my Views.py我的视图.py
from .models import Post,WebLogo
def Display_WebLogo(request):
# getting all the objects of hotel.
WebsiteLogo = WebLogo.objects.all()
return render((request, 'index.html',{'web_images' : WebsiteLogo}))
my project Urls.py我的项目 Urls.py
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my Html Code我的 HTML 代码
<a href="index.html"><img src="{{ WebLogo.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
my app url我的应用网址
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
You should write the required information in the settings.py file.您应该在 settings.py 文件中写入所需的信息。
https://docs.djangoproject.com/en/dev/ref/settings/#media-root https://docs.djangoproject.com/en/dev/ref/settings/#media-root
In HTML Code, use 'web_images' not WebLogo like this在 HTML 代码中,像这样使用“web_images”而不是 WebLogo
<a href="index.html"><img src="{{ web_images.0.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
It looks like {{ data.0 }}.它看起来像 {{ data.0 }}。 See Variables and lookups .
请参阅变量和查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.