Fcamp:การรับอินพุตจาก form
รุ่นแก้ไขเมื่อ 07:26, 25 ตุลาคม 2553 โดย Jittat (คุย | มีส่วนร่วม)
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 %}