用特定的Django任务编码思维障碍 [英] Coding mental block with specific Django task

查看:49
本文介绍了用特定的Django任务编码思维障碍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天,我一直在想这个很长一段时间,无法用我当前的工具集弄清楚如何在Django中实现它.

I've been thinking of this for a LONG time the past few days and can't figure out with my current toolset how to implement this in Django.

例如,我想要的东西可以用Java轻松实现,但是要如何在Web上实现却要困难得多

What I want is something that can be implemented trivially in Java for example, but how I'd do that on the web is much more difficult

问题

我想将数据发送到HTML模板,具体示例:

I want to send data to a HTML template, specific example:

"5 + 50是什么?"

"What is 5 + 50?"

必须动态生成数据,因此5和50实际上是随机生成的.

The data must be dynamically generated, so the 5 and 50 are actually randomly generated.

我认为我很乐于做这部分,因为我只是简单地使用views.py将随机变量传递给模板.

I think I am comfortable doing this part, as I'd simply pass a random variable to a template using the views.py

这是我不知道如何进行的地方

我希望用户能够输入答案, 并通知他们.

I want the user to be able to enter their answer, and have them notified if it correct.

为此,我需要将变量从模板传递回另一个视图函数.如果完全有可能,我不确定该怎么做.

To do this, I'd need to pass the variables from the template back to another view function. I am not sure how to do that, if it is possible at all.

这就是我决定追求自己的项目的方式,我不确定这是否是最有效的方法

This is how I'm deciding to pursue my projecti dea, and I'm not sure if this is the most efficient way

tl; dr我只是想使用Django随机生成和计算数据

tl;dr I just wanted data to be randomly generated and calculated using Django

推荐答案

一个纯django解决方案是:

A pure django solution would be:

  • 使用三个整数字段(例如 num1 num2 result )创建表单
  • 在您看来,在 num1 num2 中填充要添加的数字
  • 在您的模板中呈现表单( num1 num2 字段应为只读)
  • 用户在 result 字段中输入答案并提交表单
  • 在您看来,确定 num1 + num2 ==结果
  • 如果答案正确,则重定向到成功页面,否则重新显示表单
  • create a form with three integer fields (say, num1, num2 and result)
  • in your view, populate num1 and num2 with the numbers to be added
  • render the form in your template (num1 and num2 fields should be read only)
  • the user enters the answer in the result field and submits the form
  • in your view, determine whether num1 + num2 == result
  • redirect to a success page if the answer is correct, otherwise redisplay the form

这篇关于用特定的Django任务编码思维障碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆