I'm building a website where college students can order delivery food. One special attribute about our site is that customers have to choose a preset delivery time. For example we have a drop at 7pm, 10pm, and at midnight.
我正在建立一个大学生可以订购送餐的网站。关于我们网站的一个特殊属性是客户必须选择预设的交货时间。例如,我们在晚上7点,晚上10点和午夜有一个下降。
All the information about the food is static (ie price, description, name), except the quantity remaining for that specific drop time.
有关食物的所有信息都是静态的(即价格,描述,名称),但特定停药时间的剩余数量除外。
Obviously i didn't want to hardcode the HTML for all the food items on my menu page, so i wrote a forloop in the html template. So i need to store the quantity remaining for the specific time somewhere in my model. the only problem is that I'm scared that if i use the same variable to transport the quantity remaining number to my template, i'll give out wrong information if alot of people are accessing the menu page at the same time.
显然我不想为我的菜单页面上的所有食品硬编码HTML,所以我在html模板中写了一个forloop。所以我需要在模型中的某个地方存储剩余的特定时间的数量。唯一的问题是,我很害怕,如果我使用相同的变量将剩余数量的数量传输到我的模板,如果很多人同时访问菜单页面,我会给出错误的信息。
For example, lets say the 7pm drop has 10 burritos remaining. And the 10pm drop has 40 burritos. Is there a chance that if someone has faster internet than the other customer, the wrong quantity remaining will display?
例如,假设7pm下降剩余10个卷饼。下午10点的下降有40个卷饼。如果有人比其他客户有更快的互联网,是否有可能显示错误的剩余数量?
how would you guys go around to solve this problem? i basically need a way to tell my template the quantity remaining for that specific time. and using the solution i have now, doesn't make me feel at ease. Esp if many people are going to be accessing the site at the same time.
你们怎么解决这个问题呢?我基本上需要一种方法来告诉我的模板在特定时间剩余的数量。并且使用我现在拥有的解决方案,并不会让我感到安心。 Esp,如果有很多人同时访问该网站。
view.py
orders = OrderItem.objects.filter(date__range=[now - timedelta(hours=20), now]).filter(time=hour)
steak_and_egg = 0
queso = 0
for food in orders:
if food.product.name == "Steak and Egg Burrito":
steak_and_egg = steak_and_egg + food.quantity
elif food.product.name == "Queso Burrito":
queso = queso + food.quantity
#if burritos are sold out, then tell template not to display "buy" link
quantity_steak_and_egg = max_cotixan_steak_and_egg - steak_and_egg
quantity_queso = max_cotixan_queso - queso
#psuedocode
steakandegg.quantity_remaining = quantity_steak_and_egg
queso.quantity_remaining = quantity_queso
HTML:
{% for item in food %}
<div id="food_set">
<img src="{{item.photo_menu.url}}" alt="" id="thumbnail photo" />
<div style='overflow:hidden'>
<p id="food_name">{{item.name}}</p>
<p id="price">${{item.price}}</p>
</div>
<p id="food_restaurant">By {{item.restaurant}}</p>
<div id="food_footer">
<img src="{{MEDIA_URL}}/images/order_dots.png" alt="" id="order_dots" />
<a id ="order_button" href="{{item.slug}}"></a>
<p id="quantity_remaining">{{item.quantity_remaining}} left</p>
</div><!-- end food_footer-->
</div><!-- end food_set-->
1 个解决方案
#1
1
I don't understand what "faster Internet" or "using the same variable" have to do with anything here (or, indeed, what it has to do with sqlite particularly).
我不明白“更快的互联网”或“使用相同的变量”与这里的任何事情有什么关系(或者,实际上,它与sqlite有什么关系)。
This question is about a fundamental property of web apps: that they are request/response based. That is, the client makes a request, and the server replies with a response, which represents the status of the data at that time. There's simply no getting around that: you can make it more dynamic, by using Ajax to update the page after the initial load, which is what * does to show update messages while you're on the page. But even then, there's still a delay.
这个问题是关于网络应用程序的基本属性:它们是基于请求/响应的。也就是说,客户端发出请求,服务器回复响应,该响应表示当时数据的状态。通过使用Ajax在初始加载后更新页面,您可以使其更具动态性,这就是*在您进入页面时显示更新消息的功能。但即便如此,仍有延迟。
(I should note that there are ways of doing real-time updates, but they're complicated, and almost certainly overkill for a college food-ordering website.)
(我应该注意到有一些方法可以进行实时更新,但它们很复杂,而且对于大学的食品订购网站来说几乎肯定是过度的。)
Now the issue is, why does this matter? It shouldn't. The user sees a page saying there is 1 burrito left - perhaps with a red warning saying "order quickly! almost gone!" - and they press the order button. On submission of that order, your code presumably checks for the actual status at that time. And, guess what, in the meantime you've processed another order and the burrito has already gone. So what? You simply show a message to the user, "sorry, it's gone, try something else". Anyone with any experience ordering things on the web - say, concert tickets - will understand what's happened.
现在的问题是,为什么这很重要?它不应该。用户看到一个页面说有剩下1个墨西哥卷饼 - 也许有一个红色警告说“快速订购!几乎消失!” - 他们按下订单按钮。提交该订单后,您的代码可能会检查当时的实际状态。并且,猜猜是什么,与此同时你已经处理了另一个订单,而且卷饼已经消失了。所以呢?您只需向用户显示一条消息,“对不起,它消失了,尝试别的东西”。任何有网络订购经验的人 - 比如音乐会门票 - 都会明白发生了什么。
#1
1
I don't understand what "faster Internet" or "using the same variable" have to do with anything here (or, indeed, what it has to do with sqlite particularly).
我不明白“更快的互联网”或“使用相同的变量”与这里的任何事情有什么关系(或者,实际上,它与sqlite有什么关系)。
This question is about a fundamental property of web apps: that they are request/response based. That is, the client makes a request, and the server replies with a response, which represents the status of the data at that time. There's simply no getting around that: you can make it more dynamic, by using Ajax to update the page after the initial load, which is what * does to show update messages while you're on the page. But even then, there's still a delay.
这个问题是关于网络应用程序的基本属性:它们是基于请求/响应的。也就是说,客户端发出请求,服务器回复响应,该响应表示当时数据的状态。通过使用Ajax在初始加载后更新页面,您可以使其更具动态性,这就是*在您进入页面时显示更新消息的功能。但即便如此,仍有延迟。
(I should note that there are ways of doing real-time updates, but they're complicated, and almost certainly overkill for a college food-ordering website.)
(我应该注意到有一些方法可以进行实时更新,但它们很复杂,而且对于大学的食品订购网站来说几乎肯定是过度的。)
Now the issue is, why does this matter? It shouldn't. The user sees a page saying there is 1 burrito left - perhaps with a red warning saying "order quickly! almost gone!" - and they press the order button. On submission of that order, your code presumably checks for the actual status at that time. And, guess what, in the meantime you've processed another order and the burrito has already gone. So what? You simply show a message to the user, "sorry, it's gone, try something else". Anyone with any experience ordering things on the web - say, concert tickets - will understand what's happened.
现在的问题是,为什么这很重要?它不应该。用户看到一个页面说有剩下1个墨西哥卷饼 - 也许有一个红色警告说“快速订购!几乎消失!” - 他们按下订单按钮。提交该订单后,您的代码可能会检查当时的实际状态。并且,猜猜是什么,与此同时你已经处理了另一个订单,而且卷饼已经消失了。所以呢?您只需向用户显示一条消息,“对不起,它消失了,尝试别的东西”。任何有网络订购经验的人 - 比如音乐会门票 - 都会明白发生了什么。