1

A noobish question to be sure.

<a href="{% url 'stuff.views.SomeView' %}/somethingnew">
    <button>See something new on this page</button>
</a>

<form action="" method="post">{% csrf_token %}
    <button name="somethingnew" type="submit" value=True>See something new on this page</button>
</form>

With either choice, I update some boolean variable, perform the appropriate calculations, call the page view and render a page with something new on this page. Part of the reason I use either method is to save the state of a collection of boolean variables. What is the best way 1) change a boolean variable 2) save its state 3) perform the necessary updates when the button is clicked and finally 4) render page after the underlying data has been updated?

Right now, I am using forms rather than links so that I don't need to code a url for each boolean variable. Which method is better? Will one method improve the time it takes to reload the page (assuming many boolean variables)?

4

1 回答 1

1

1) 遵循 REST 思维方式,POST 请求是为了传输用户输入,因为您正在更改数据库对象。

2)如果永远不需要输入(会话持续时间),我会将其保存在 Session 对象中。否则,就像您现在所做的那样在数据库中。

3/4)我会在一个表格中收集所有必要的信息。当用户在 POST 请求中提交表单时,我会计算数据并使用包含计算结果的呈现页面进行响应。如果输入变量是通过间歇性计算逐步收集的,我只需相应地更新输入表单(在组合框中显示不同的选择或类似的东西)。当然,传输也可以通过 AJAXy 方式完成。

于 2013-01-04T21:35:01.083 回答