python - 无法通过 django 中的 CRUD 操作结果显示图像
问题描述
我正在开发酒店预订应用程序,在此我想根据用户输入的位置显示酒店的图像。在此,如果我显示所有酒店,我可以显示图像,如果我试图通过一些 CRUD 操作显示图像,我无法显示它。这是我的代码片段。
class Customer_details(models.Model):
Name = models.CharField(max_length=128)
Age = models.IntegerField()
Mobile_number = models.IntegerField()
Email = models.EmailField()
Address = models.CharField(max_length=50)
Special_request = models.CharField(max_length=50, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.Name
hotel_rating_choices = (('1','1'), ('2','2'), ('3','3'), ('4','4'), ('5','5 '), ('6','6'), ('7','7'), )
class Hotel(models.Model):
Hotel_Name = models.CharField(max_length=50)
location = models.CharField(max_length=20)
no_of_rooms = models.IntegerField()
rating = models.CharField(max_length=1, choices=hotel_rating_choices, default=3)
hotel_img = models.ImageField(upload_to='hotel_images/')
uploaded_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.Hotel_Name
class Hotel_image(models.Model):
hotel_img = models.ImageField(upload_to = 'hotel_images/')
hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE)
def __str__(self):
return self.hotel
忽略其余模型,只专注于酒店模型。
`下面的代码片段是我对查询的看法。
def get_hotels_by_location(request):
location = request.POST.get('location')
print(location)
location = location.lower()
result = Hotel.objects.values('Hotel_Name', 'rating', 'hotel_img').filter(location=location)
context = {'hotels': result, 'header': ['Hotel_Name', 'Rating', 'image'] }
return render(
request,
template_name='display_hotel_by_location.html',
context=context
)
下面是我的 django html 模板
<table class="table">
<tr>
{% for i in header %}
<th>
{{ i }}
</th>
{% endfor %}
</tr>
{% for element in hotels %}
<tr>
{% with i=0 %}
{% for key, value in element.items %}
{% if i == 2 %}
<td><a href="{{ element.url }}"> <img src="{{ element.url }}" width = "300" height="300"></a> </td>
{% endif %}
<td> {{ value }} </td>
{% with j=i %}
j=j+1
i=j
{% endwith %}
{% endfor %}
</tr>
{% endfor %}
</table>
请在这个问题上帮助我。
解决方案
更改您的 Hotel_image 类以在您的外国添加相关名称以进一步使用它
class Hotel_image(models.Model):
hotel_img = models.ImageField(upload_to = 'hotel_images/')
hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE, related_name="hotel_images")
清理一下你的观点,在这种情况下你真的不需要使用值。
from .models import Hotel
def get_hotels_by_location(request):
location = request.POST.get('location').lower()
result = Hotel.objects.filter(location=location)
context = {'hotels': result, 'header': ['Hotel_Name', 'Rating', 'image'] }
return render(
request,
template_name='display_hotel_by_location.html',
context=context
)
HTML 1 - 如果您使用 Hotel 类的 hotel_img ,请使用这个
<table class="table">
<tr>
{% for i in header %}
<th>{{ i }}</th>
{% endfor %}
</tr>
{% for hotel in hotels %}
<tr>
{% if forloop.counter0 == 2 and hotel.hotel_img %}
<td><a href="{{ hotel.hotel_img.url }}"> <img src="{{ hotel.hotel_img.url }}" width="300" height="300"></a> </td>
{% endif %}
<td> {{ hotel.Hotel_Name }} </td>
</tr>
{% endfor %}
</table>
HTML 2 - 如果您使用 Hotel_image 类,请像这样使用
<table class="table">
<tr>
{% for i in header %}
<th>{{ i }}</th>
{% endfor %}
</tr>
{% for hotel in hotels %}
<tr>
{% for img in hotel.hotel_images %}
{% if img.hotel_img %}
<td><a href="{{ img.hotel_img.url }}"> <img src="{{ img.hotel_img.url }}" width="300" height="300"></a> </td>
{% endif %}
{% endfor %}
<td> {{ hotel.Hotel_Name }} </td>
</tr>
{% endfor %}
</table>
有关 Django 关系的更多信息:https ://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey.related_name
您也可以使用模板标签来过滤 Hotel_Image 中的所有图像 1 - 创建一个名为templatetag的文件夹,并在其中的一个文件中作为您的应用程序文件夹中的模板标签
酒店/模板标签/hotel_templatetag.py
在文件里面放那个代码
from django import template
from .models import Hotel_image
register = template.Library()
def get_hotel_images(self):
return Hotel_image.objects.filter(id=self.id)
你的 HTML 应该是这样的
<table class="table">
<tr>
{% for i in header %}
<th>{{ i }}</th>
{% endfor %}
</tr>
{% for hotel in hotels %}
<tr>
{% for img in hotel|get_hotel_images %}
{% if img.hotel_img %}
<td><a href="{{ img.hotel_img.url }}"> <img src="{{ img.hotel_img.url }}" width="300" height="300"></a> </td>
{% endif %}
{% endfor %}
<td> {{ hotel.Hotel_Name }} </td>
</tr>
{% endfor %}
</table>
有关模板标签的更多信息:https ://docs.djangoproject.com/en/2.0/howto/custom-template-tags/
推荐阅读
- javascript - 拆分和迭代数组并匹配一个值
- flutter - 如何将快照数据传递给 Flutter 浮动操作按钮?
- express - 通过 socket.io 访问 express-session cookie
- c# - 如何将一个线程拆分为几个单独的线程以提高 CPU 工作效率?
- node.js - PUT 请求未通过 mongdb
- python-3.x - Google API,名称 io 未定义
- angular - 点击时不显示动态模板
- python - 循环遍历 pandas 数据框以使用 for 循环替换现有值
- javascript - 等待功能条件无法正常工作
- string - Xslt 用分隔符连接两个节点列表