Django:使用POST或链接,这是更好的做法?

时间:2022-12-20 02:45:53

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?

无论选择哪种,我都会更新一些布尔变量,执行适当的计算,调用页面视图并在此页面上使用新内容呈现页面。我使用任一方法的部分原因是为了保存布尔变量集合的状态。什么是最好的方法1)更改布尔变量2)保存其状态3)单击按钮时执行必要的更新,最后4)在基础数据更新后呈现页面?

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)?

现在,我使用的是表单而不是链接,因此我不需要为每个布尔变量编写一个url代码。哪种方法更好?一种方法会改善重新加载页面所需的时间(假设有许多布尔变量)吗?

1 个解决方案

#1


1  

1) Following the REST mindset, a POST request is in order to transmit the user input, since you are altering database objects.

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

2) I'd save it in the Session object if the input is not needed forever (session duration). Otherwise in the database as you are doing now.

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

3/4) I'd gather all the necessary info in a form. When the user commits the form in a POST request, I'd compute the data and respond with the rendered page containing the computed result. If the input variables are gathered step by step with intermittent computation, I'd just update the input form accordingly (display different choices in a combo box or something like that). Of course the transmitting could be done in an AJAXy way, too.

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

#1


1  

1) Following the REST mindset, a POST request is in order to transmit the user input, since you are altering database objects.

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

2) I'd save it in the Session object if the input is not needed forever (session duration). Otherwise in the database as you are doing now.

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

3/4) I'd gather all the necessary info in a form. When the user commits the form in a POST request, I'd compute the data and respond with the rendered page containing the computed result. If the input variables are gathered step by step with intermittent computation, I'd just update the input form accordingly (display different choices in a combo box or something like that). Of course the transmitting could be done in an AJAXy way, too.

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