简体   繁体   English

从模板传递数据以在Django中查看

[英]Pass data from template to view in Django

I'm building a bus reservation website using Django. 我正在使用Django构建公交预订网站。 After user searches buses on a specific route, a list is displayed showing available buses. 用户在特定路线上搜索公共汽车后,将显示一个列表,显示可用的公共汽车。 Each list item ie bus has a book button which leads to new page called 'seats.html'. 每个列表项(例如bus)都有一个预订按钮,该按钮会转到名为“ seats.html”的新页面。 I want to pass the bus number and other information to the view of 'seats' so that I can display the number of seats available and javascript can also use this info. 我想将公交车号和其他信息传递到“座位”视图,以便我可以显示可用座位数,而javascript也可以使用此信息。 Here is my code 这是我的代码

views.py 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)

select.html select.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 %}

As you can see query set gets passed in select.html where I use for loop to display all the buses available along with their information. 如您所见,查询集在select.html中传递,我在其中使用for循环显示所有可用的总线及其信息。 I then click on book to book seats of my desired bus choice. 然后,我单击“预定”以预定所需公共汽车的座位。 I want that when I click on book, the bus information also gets passed to the next template ie seats.html so that I use that information to display available seats and other related info. 我希望当我单击书时,公交信息也将传递到下一个模板即seat.html,以便我使用该信息显示可用的座位和其他相关信息。 Here are some screenshots to help you guys get a better picture. 以下是一些屏幕截图,可帮助您获得更好的画面。

这是用户输入其查询的主页 显示可用的巴士清单

显示座位

As you can see in the first image user enters his query. 如您所见,在第一个图像中,用户输入了他的查询。 Then a list of buses gets displayed in the second image or in this case just one bus. 然后,在第二个图像中显示公交车列表,在这种情况下,仅显示一辆公交车。 Then he click book on that bus and a seating chart is displayed. 然后,他单击该公共汽车上的书,并显示座位表。 How can I pass info from 'select.html' template to seats view. 如何将信息从“ select.html”模板传递到座位视图。 Please don't suggest to pass info using url. 请不要建议使用网址传递信息。 It can lead to manipulation very easily. 它很容易导致操纵。

Please don't suggest to pass info using url. 请不要建议使用网址传递信息。 It can lead to manipulation very easily. 它很容易导致操纵。

Sadly, every other way to pass the information to the view is not that much better or more secure. 可悲的是,将信息传递给视图的所有其他方式并没有更好或更安全。

Never ever trust user input. 永远不要相信用户输入。

No matter what way you choose in the end, you will have to validate the input. 无论最终选择哪种方式,都必须验证输入。

And using URLs has some advantages. 使用URL具有一些优势。 For example: You can send a link to the route you just found to a friend (so called deep link). 例如:您可以将指向您刚刚找到的路线的链接发送给朋友(所谓的深层链接)。 Otherwise your friend would have to click through your form by himself. 否则,您的朋友将不得不亲自点击您的表单。


If you are still interested in doing this by URL you could do it as follows: 如果您仍然对通过URL执行此操作感兴趣,可以按照以下步骤进行操作:

You could change your url schema for the URL that you called book:seats to contain all of the required information. 您可以更改名为book:seats的URL的url模式,以包含所有必需的信息。 Assume that book:seat looks like the following in your urls.py : 假设book:seat看起来像您的urls.py的以下内容:

url(r'^book/seats/$', views.seats, name='book:seats')

You chould change this URL pattern to contain all of the information you want to pass to the seats view: 您应该更改此URL模式,以包含要传递到seats视图的所有信息:

url(r'^book/seats/(?P<bus_number>\w+)/(?P<route>\w+)/(?P<date>\w+)/(?P<time>\w+)$', views.seats, name='book:seats')

This would add a so called named group to your URL pattern. 这会将一个所谓的命名组添加到您的URL模式。 You can read about named groups here: named groups documentation . 您可以在此处阅读有关命名组的信息: 命名组说明文件 Obviously, you will have to modify this example URL pattern to fit your needs. 显然,您将不得不修改此示例URL模式以适合您的需求。

Next step is to update your view function to handle all those extra view arguments: 下一步是更新您的视图函数,以处理所有这些额外的视图参数:

def seats(request, bus_number, route, date, time):
    template = 'seats.html'
    context = {}
    return render(request, template, context)

The last step is to modify the way you render the link in your template: 最后一步是修改在模板中呈现链接的方式:

<a href="{% url 'book:seats' info.bus_number info.route info.date info.time %}">Book</a>

You have to add the arguments in the same order as they are used in the seats function. 您必须按照在seats函数中使用的顺序添加参数。 You can read more about this in the official documentation here: template tag documentation . 您可以在以下官方文档中了解更多相关信息: 模板标签文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM