我正在开发酒店预订应用程序,因此我想根据用户输入的位置显示酒店的图像。在这种情况下,如果我正在显示所有酒店,那么我可以显示图像,如果我尝试通过某些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>
请帮助我解决这个问题。
答案 0 :(得分:0)
更改您的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 的文件夹,并在其中的一个文件内将其作为您应用文件夹中的templatetag
hotel / templatetags / 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/