英文:
Django - display image from static folder in accordance to values from DB
问题
我有一个Django
应用程序,其中有一个模型用于保存城市、县和国家的代码:
models.py
class AreaMap(models.Model):
Code = models.AutoField(primary_key=True)
fCityCode = models.CharField(
verbose_name='City', max_length=100)
fCountyCode = models.CharField(
verbose_name='County', max_length=100)
fCountryCode = models.CharField(
verbose_name='Country', max_length=100)
def __str__(self):
return self.fCityCode
对于每个城市,我在静态文件夹中有一些静态图像,这些图像不会更改(或者至少如果需要,旧文件将被新文件覆盖,但保持相同的文件名)。图像与数据库中的值之间的对应关系是通过字段fCityCode
来实现的:
示例:
对于纽约 - 在数据库中保存为'NewYork',静态文件夹中的图像文件名也是相同的,'NewYork'。
现在,我想在我的HTML
模板中显示与数据库值对应的文件夹中的每个图像。
我已经进行了一些研究,并且了解到,当涉及到静态图像时,最好从静态文件夹中导入它们,而不是将图像存储在数据库中,这是有道理的。是否有一种方法可以进行此映射并在Django
中实现这一点?谢谢
稍后编辑
所以,我的settings.py
如下所示:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
我的urls.py
如下所示:
urlpatterns = [
#其他URL
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
上面的模型已根据foo-bar
的答案进行了修改,并添加了@property
构造函数:
class AreaMap(models.Model):
Code = models.AutoField(primary_key=True)
fCityCode = models.CharField(
verbose_name='City', max_length=100)
fCountyCode = models.CharField(
verbose_name='County', max_length=100)
fCountryCode = models.CharField(
verbose_name='Country', max_length=100, default='RO')
@property
def img_url(self):
return "{}{}".format(settings.MEDIA_URL, self.fCityCode)
使用浏览器的检查选项,这是照片的URL
看起来像,但不知何故它在浏览器中没有显示:
<a href="/path/" class="card img-card fh-400 border-0 outer-bg" data-background-inner="/media/NewYork.jpg">
<div class="inner-bg overlay-dark" style="background-image:url('/media/NewYork.jpg');">
</div>
</a>
如果您有其他问题,请随时提出。
英文:
I have a Django
app which has a model that holds cities, counties and countries codes:
models.py
class AreaMap(models.Model):
Code = models.AutoField(primary_key=True)
fCityCode = models.CharField(
verbose_name='City', max_length=100)
fCountyCode = models.CharField(
verbose_name='County', max_length=100)
fCountryCode = models.CharField(
verbose_name='Country', max_length=100)
def __str__(self):
return self.fCityCode
For each city, I have some static images in my static folder which are not going to be changed (or at least, if required, the old file would be overwritten with a new one but keeping the same filename). The correspondence between the image and the value from the database is by field 'fCityCode`:
Example:
For New York - in the database is saved as 'NewYork' and the image filename from my static folder is the same, 'NewYork'.
Now, I would like to display in my HTML
template every image from the folder that corresponds to a value from the database.
I've done some research and I read that when it comes to static images is better to import them from the static folder rather than storing the images in the database which makes sense. Is there any way of doing this mapping and achieving this using Django
? Thanks
Later edit
So, my settings.py
looks as below:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
My urls.py
looks as below:
urlpatterns = [
#other urls
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
The model above have been modified according to foo-bar
answer and added the @property
constructor:
class AreaMap(models.Model):
Code = models.AutoField(primary_key=True)
fCityCode = models.CharField(
verbose_name='City', max_length=100)
fCountyCode = models.CharField(
verbose_name='County', max_length=100)
fCountryCode = models.CharField(
verbose_name='Country', max_length=100, default='RO')
@property
def img_url(self):
return "{}{}".format(settings.MEDIA_URL, self.fCityCode)
Using inspect option from the browser, this is how the URL
of the photo looks like but somehow it is not displayed in the browser:
<a href="/path/" class="card img-card fh-400 border-0 outer-bg" data-background-inner="/media/NewYork.jpg">
<div class="inner-bg overlay-dark" style="background-image:url(&quot;/media/NewYork.jpg&quot;);">
</div>
答案1
得分: 1
根据您的设置,您的目录结构应如下所示:
.
├── project_root
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── media
│ └── foo
└── myapp_name
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ ├── 0001_initial.py
│ └── __init__.py
├── models.py
├── templates
│ └── sample.html
├── urls.py
└── views.py
关键点是 media
目录应放在根目录中。
要在模板中呈现图像,您需要通过视图将 AreaMap
查询集传递给模板。
#myapp_name/views.py
from django.shortcuts import render
def sample_view(request):
return render(request, 'sample.html', context={'qs': AreaMap.objects.all()})
然后在模板中迭代(或在模板中执行其他操作)查询集:
#myapp_name/templates/sample.html
{% for item in qs %}
<p><img src="{{ item.img_url }}" width="300px"></p>
{% endfor %}
英文:
You are almost there !!
According to your settings, your directory structure should be like this,
.
├── project_root
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── media
│   └── foo
└── myapp_name
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   ├── 0001_initial.py
│   └── __init__.py
├── models.py
├── templates
│   └── sample.html
├── urls.py
└── views.py
Key Point is the media
directory should be placed in the root director
To render the images in your template, you need to pass the AreaMap
queryset to the template through view.
#myapp_name/views.py
from django.shortcuts import render
def sample_view(request):
return render(request, 'sample.html', context={'qs': AreaMap.objects.all()})
and now iterate (or whatever you want to do in your template) the Queryset in the template as,
#myapp_name/templates/sample.html
{% for item in qs %}
<p><img src="{{ item.img_url }}" width="300px"></p>
{% endfor %}
答案2
得分: 1
这些文件是固定的,不会经常更改,我认为它们应该存储在静态目录
中。现在,如果文件夹结构如下:
.
├── project_root
├── static
│ ├── city_images
│ ├── LA.jpg
│ ├── NewYork.jpg
│ └── Washington.jpg
├── apps
那么您的设置应该如下:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
然后只需使用:
{% load static %}
<div class="inner-bg overlay-dark" style="background-image:url({% static 'city_images/NewYork.jpg' %});">
或者您可以像在您的编辑中提到的那样与您的模型集成:
# 模型
from django.templatetags.static import static
class AreaMap(models.Model):
Code = models.AutoField(primary_key=True)
fCityCode = models.CharField(
verbose_name='City', max_length=100)
fCountyCode = models.CharField(
verbose_name='County', max_length=100)
fCountryCode = models.CharField(
verbose_name='Country', max_length=100, default='RO')
@property
def img_url(self):
return static("city_images/{}".format(self.fCityCode))
# 视图
{% for i in areamap_objects %}
<img src="{{ i.img_url }}" >
{% endfor %}
英文:
As this files are fixed and won't be changed frequently, I think they should be stored in static directory
. Now if the folder structure is like this:
.
├── project_root
├── static
│ ├── city_images
│ ├── LA.jpg
│ ├── NewYork.jpg
│ └── Washington.jpg
├── apps
Then your settings should be like this:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
Then simply use:
{% load static %}
<div class="inner-bg overlay-dark" style="background-image:url({% static 'city_images/NewYork.jpg' %};);">
Or you can integrate with your model like mentioned in your edit:
# model
from django.templatetags.static import static
class AreaMap(models.Model):
Code = models.AutoField(primary_key=True)
fCityCode = models.CharField(
verbose_name='City', max_length=100)
fCountyCode = models.CharField(
verbose_name='County', max_length=100)
fCountryCode = models.CharField(
verbose_name='Country', max_length=100, default='RO')
@property
def img_url(self):
return static("city_images/{}".format(self.fCityCode))
# view
{% for i in areamap_objects %}
<img src="{{ i.img_url }}" >
{% endfor %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论