【发布时间】:2017-04-28 18:07:15
【问题描述】:
我正在使用 Django 构建一个巴士预订网站。用户在特定路线上搜索巴士后,会显示一个显示可用巴士的列表。每个列表项,即公共汽车都有一个书籍按钮,该按钮指向名为“seats.html”的新页面。我想将公交车号和其他信息传递给“座位”视图,以便我可以显示可用座位数,并且 javascript 也可以使用此信息。这是我的代码
views.py
def find_bus(request):
form = forms.FormFindBus
template = 'home.html'
context = {'form': form}
return render(request, template, context)
def select(request):
bus_type = request.GET.get('bus_type')
bus_from = request.GET.get('bus_from')
bus_to = request.GET.get('bus_to')
date_string = request.GET.get('date')
qs = Bus.objects.filter(type_of_bus=bus_type, route__location_from=bus_from, route__location_to=bus_to, date=date_string)
context = {'qs': qs,}
template = 'select.html'
return render(request, template, context)
def seats(request):
template = 'seats.html'
context = {}
return render(request, template, context)
选择.html
{% for info in qs %}
<a href="{% url 'book:seats' %}">Book</a>
<ul>
<li><b>Bus Number -</b> {{ info.bus_number }}</li>
<li><b>Route -</b> {{ info.route }}</li>
<li><b>Date -</b> {{ info.date }}</li>
<li><b>Time -</b>{{ info.time }}</li>
</ul>
</div>
{% endfor %}
如您所见,查询集在 select.html 中传递,我在其中使用 for 循环显示所有可用的总线及其信息。然后我点击预订以预订我想要的巴士选择的座位。我希望当我点击书本时,巴士信息也会传递到下一个模板,即 seat.html,以便我使用该信息来显示可用座位和其他相关信息。这里有一些截图可以帮助你们获得更好的图片。
正如您在第一张图片中看到的那样,用户输入了他的查询。然后在第二张图像中显示公共汽车列表,或者在本例中仅显示一辆公共汽车。然后他点击那辆公共汽车上的书,并显示座位表。如何将“select.html”模板中的信息传递到席位视图。请不要建议使用 url 传递信息。它很容易导致操纵。
【问题讨论】:
标签: javascript html django