目前我恢复了第三个下拉列表(Extract_single_city)的选定值,如下所示:
city = request.GET.get("extract_single_city")
仍然使用request
,我想检索(例如)马德里和巴塞罗那的值,创建两个变量:departure
和destination
。例如,departure
将是马德里,destination
将是巴塞罗那。
正如您从照片中的下拉列表中看到的,我可以在第二个下拉列表(
Trip
)或第三个下拉列表(extract_single_city
)中恢复两个值:这是同一件事,但我认为更容易从第二个下拉列表中恢复值。不同之处在于,在第二个下拉列表中,值由连字符 (-) 连接,因此必须将它们分开,而在第三个下拉列表中,值以已经分开且单独的方式填充,但可能更难以恢复他们从这里来。
我正在使用这个解决方案与
global
,但我不喜欢它,我想使用一些与request
global departure, destination
departure = trip.city_departure.name,
destination = trip.city_destination.name
或者我正在使用这个(但它不起作用)
departure = request.GET.get("trips").split("-")[0] #or split("-", 0)
destination = request.GET.get("trips").split("-")[1] #or split("-", 1)
views.py
def trip_selector(request):
if "Hx-Request" in request.headers:
trips = FullRecord.objects.none()
if request.headers["Hx-Trigger"] == "id_trip":
country_id = request.GET.get("country")
if country_id != "":
trips = FullRecord.objects.filter(country_id=country_id)
return render(request, "trips.html", {"trips": trips})
elif request.headers["Hx-Trigger"] == "id_extract_single_city":
selected_trip = request.GET.get("trips")
extracted_cities = []
if selected_trip != "":
trip = FullRecord.objects.get(id=int(selected_trip))
trip_with_combined_names = trip.departure_destination
split_trip = trip_with_combined_names.split("-")
extracted_cities = split_trip
return render(request, "extract_single_city.html", {"options": extracted_cities})
else:
countries = Country.objects.all()
return render(request, "form.html", {"countries": countries})
forms.html
{% extends 'base.html' %}
{% block main-content %}
<div class="flex justify-between px-10 gap-5 mt-5">
<div class="flex flex-col w-full">
<!-- First Combobox -->
<label for="id_country">Country</label>
<select name="country" id="id_country">
<option value="">Please select</option>
{% for country in countries %}
<option value="{{ country.pk }}">{{ country.name }}</option>
{% endfor %}
</select>
</div>
<div class="flex flex-col w-full">
<!-- Second Combobox ??????? (non lo so)-->
<label for="id_trip">Trip</label>
<select name="trips" id="id_trip"
hx-get="{% url 'trips' %}"
hx-include="[name=country]"
hx-indicator=".htmx-indicator"
hx-trigger="change from:#id_country">
</select>
</div>
<!-- Third Combobox -->
<div class="flex flex-col w-full">
<label for="id_extract_single_city">Extract single city</label>
<select name="extract_single_city" id="id_extract_single_city"
hx-get="{% url 'trips' %}"
hx-include="[name=trips]"
hx-indicator=".htmx-indicator"
hx-trigger="change from:#id_trip">
</select>
</div>
</div>
<div class="flex flex-col w-1/2 justify-end">
<button class="bg-green-300 p-2 rounded"
type="button"
hx-get="{% url 'result' %}"
hx-include="#id_extract_single_city"
hx-target="#id_result"> Submit
<!-- hx-swap="beforeend">Submit -->
</button>
</div>
<div class="py-2 px-10 h-96 mt-5">
<textarea name="result" id="id_result" readonly class="w-full h-full rounded px-2 py-3"></textarea>
</div>
{% endblock main-content %}
trips.html
<option value="">Please select</option>
{% for trip in trips %}
<option value="{{ trip.id }}">{{ trip.city_departure }}-{{ trip.city_destination }}</option>
{% endfor %}
extract_single_city.html
<option value="">Please select</option>
{% for i in options %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
模型.py
from django.db import models
class Country(models.Model):
name = models.CharField(max_length=40)
def __str__(self):
return self.name
class City(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE)
name = models.CharField(max_length=40)
def __str__(self):
return self.name
class FullRecord(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE)
city_departure = models.ForeignKey(City, on_delete=models.CASCADE, related_name="city_departure")
city_destination = models.ForeignKey(City, on_delete=models.CASCADE, related_name="city_destination")
@property
def departure_destination(self):
return f"{self.city_departure}-{self.city_destination}"
def __str__(self):
return self.country.name
谢谢!!!
split功能不起作用的原因是你从HTML带来的数据是“值”。当您使用
request.GET.get("trips")
时,它不会让您 {{ trip.city_departure }}-{{ trip.city_destination }}
。带上这个:{{ trip.id }}
,因为这是你的“价值”。