无法从数据库 Django 框架获取图像
Cannot Get image From Database Django Framework
我想在管理面板中创建一个设置,这将帮助我从管理面板更改网站徽标。我为数据库创建了一个模型,它正在工作并将图像上传到数据库+文件夹。但我无法将此图像添加到网站模板中。当我查看页面源图像时 SCR 是空的。
我的Model.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
我的Views.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}))
我的项目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)
我的Html代码
<a href="index.html"><img src="{{ WebLogo.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
我的应用程序url
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
您应该在 settings.py 文件中写入所需的信息。
https://docs.djangoproject.com/en/dev/ref/settings/#media-root
在HTML代码中,使用'web_images'而不是像这样的WebLogo
<a href="index.html"><img src="{{ web_images.0.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
看起来像{{ data.0 }}。参见 Variables and lookups。
我想在管理面板中创建一个设置,这将帮助我从管理面板更改网站徽标。我为数据库创建了一个模型,它正在工作并将图像上传到数据库+文件夹。但我无法将此图像添加到网站模板中。当我查看页面源图像时 SCR 是空的。
我的Model.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
我的Views.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}))
我的项目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)
我的Html代码
<a href="index.html"><img src="{{ WebLogo.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
我的应用程序url
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
您应该在 settings.py 文件中写入所需的信息。
https://docs.djangoproject.com/en/dev/ref/settings/#media-root
在HTML代码中,使用'web_images'而不是像这样的WebLogo
<a href="index.html"><img src="{{ web_images.0.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
看起来像{{ data.0 }}。参见 Variables and lookups。