ผลต่างระหว่างรุ่นของ "Fcamp:การรับอินพุตจาก form"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย '== views.py == <pre> from django.shortcuts import render_to_response from random import sample, randint def index(request): notice =…') |
Jittat (คุย | มีส่วนร่วม) |
||
| แถว 23: | แถว 23: | ||
'b': b, | 'b': b, | ||
'notice': notice }) | 'notice': notice }) | ||
| + | </pre> | ||
| + | |||
| + | == index.html == | ||
| + | |||
| + | <pre> | ||
| + | {% extends "base.html" %} | ||
| + | |||
| + | {% block content %} | ||
| + | <h1>Answer this!</h1> | ||
| + | <p> | ||
| + | <b>{{ notice }}</b><br/> | ||
| + | <form method="post"> | ||
| + | {{ a }} + {{ b }} = | ||
| + | <input type="text" name="answer"/> | ||
| + | <input type="hidden" name="a" value="{{ a }}"/> | ||
| + | <input type="hidden" name="b" value="{{ b }}"/> | ||
| + | <input type="submit" value="Go"/> | ||
| + | </form> | ||
| + | </p> | ||
| + | {% endblock %} | ||
</pre> | </pre> | ||
รุ่นแก้ไขเมื่อ 07:26, 25 ตุลาคม 2553
views.py
from django.shortcuts import render_to_response
from random import sample, randint
def index(request):
notice = ''
if request.method == 'POST':
a = int(request.POST['a'])
b = int(request.POST['b'])
answer = int(request.POST['answer'])
if a+b == answer:
notice = 'Correct answer'
else:
notice = 'Sorry, please try again'
else:
a = randint(1,100)
b = randint(1,100)
return render_to_response("index.html",
{ 'a': a,
'b': b,
'notice': notice })
index.html
{% extends "base.html" %}
{% block content %}
<h1>Answer this!</h1>
<p>
<b>{{ notice }}</b><br/>
<form method="post">
{{ a }} + {{ b }} =
<input type="text" name="answer"/>
<input type="hidden" name="a" value="{{ a }}"/>
<input type="hidden" name="b" value="{{ b }}"/>
<input type="submit" value="Go"/>
</form>
</p>
{% endblock %}