将复选框状态与查询结果保留在同一页面上
我正在使用 django,并且有一个带有 GET 表单的静态网页和大约 30 个复选框。 用户选择各个框,然后单击搜索,数据库中的结果将返回到表中的同一页面。 然而,所有复选框都已被清除,因为 HTML 是无状态的。
“记住”复选框状态的最简单的解决方案是什么? 我浏览了 django 文档,但示例仅使用变量和 IF 作为单个文本表单。 当然,我不必检查查询字典中是否设置了每个复选框? 另外,我的表单是自定义创建的,而不是使用 django 表单。
最终,我计划对页面使用 JSON,并且只更新表格,这将解决这个问题,但我不确定它是否会引入更多问题,例如后退按钮不适用于以前的搜索。
I'm using django, and have a static webpage with a GET form, and about 30 checkboxes. The user selects various boxes, and clicks search, and a result from a database is returned to the same page in a table. However, all the checkboxes have been cleared, since HTML is stateless.
What's the simplest solution to 'remember' the state of the checkboxes? I've looked through django docs, but the examples just use a variable and an IF for a single text form. Surely, I dont have to check if each checkbox is set in the querydict? Also, my form was custom created, rather than using django forms.
Eventually, i was planning to use JSON for the page and only update the table, and that would solve this problem, but Im not sure if it would introduce many more, eg back button not working for previous searches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“‘记住’复选框状态的最简单解决方案是什么?”
Web 事务是无状态的。
有几种常见的方式来实现状态。
会话。 如果您的用户已登录,他们就会有一个会话。 在他们的会话中存储东西。 他们发布的表单进入会话。 然后可以检索该信息,以便将新表单发回给他们。
饼干。 会话使用 cookie。 您可以手动使用cookie。 您在这里有两个选择。
将表单复选框信息放入cookie中。 将其与响应一起发送下来。 检查每个请求中的 cookie,以便您知道使用什么作为表单的种子。
将会话密钥放入 cookie 中。 这就是 Django 为你所做的。 不要自己实现这个。
URL 中的查询字符串。 您有与 cookie 相同的两种选择。 要么将设置设置为旧的 base64 字符串,要么传递代表会话的 ID。
"What's the simplest solution to 'remember' the state of the checkboxes?"
Web transactions are stateless.
There are several common ways to implement state.
Sessions. If your user is logged in, they have a session. Store stuff in their session. The form they POST goes into the session. This can then be retrieved in order to post a new form back to them.
Cookies. Sessions use cookies. You can use cookies manually. You have two choices here.
Put the form checkbox information into the cookie. Send it down with the response. Check for the cookie in each request so you know what to seed the form with.
Put a session key in the cookie. This is what Django does for you. Don't implement this yourself.
Query strings in the URL. You have the same two choices as with cookies. Either make the settings a big-old base64 string or pass an ID that stands in for the session.
有多种方法可以解决后退按钮问题(哈希标签以及附加到 URL 的内容)。 然而,我一直都是通过这种方式来实现这一目标的。 本质上,我会在 POST 中将复选框作为数组传递,然后,当您制作它们时,只需创建一个循环来制作您需要的所有框,然后检查 POST 以查看它是否已被选中。 除此之外,我不知道。
There are ways to fix the back button issue (hash tags and whatnot appended to an URL). However, the way that I have always accomplished this is through just that. Essentially, I would pass the checkboxes as an array in POST and then, when you go to make them, just make a loop that will make all the boxes you need, and check against POST to see if it's been checked or not. Other than that, I don't know.
使用获取? 好的。 访问复选框的方式与访问文本字段数据的方式相同。
request.GET['yourfield']
使用 print 语句检查 GET 中可用的信息。
打印 request.GET['yourfield']
Using GET? Good. Access the check boxes the same way you access textfield data.
request.GET['yourfield']
Use print staments to inspect the information available in GET.
print request.GET['yourfield']
如果您使用的是 Form 对象,那么您只需将请求传递给构造函数即可完成。 如果您手动执行此操作,则必须对会话进行一些操作。
If you were using a Form object, then you could just pass the constructor the request and be done with it. If you're doing it manually, you'll have to do some finangling with the session.