printing random numbers in appropriate range

This commit is contained in:
Stephen McQuay 2013-04-23 19:45:32 -07:00
parent d3c9e3e5aa
commit 6542c63ec3
5 changed files with 104 additions and 18 deletions

34
mmg/forms.py Normal file
View File

@ -0,0 +1,34 @@
from django import forms
class NumberForm(forms.Form):
first = forms.IntegerField(
widget=forms.TextInput(
attrs={
'class': 'number',
'size': 2,
'readonly': True,
'hidden': True,
}))
operation = forms.CharField(
widget=forms.TextInput(
attrs={
'class': 'number',
'size': 2,
'readonly': True,
'hidden': True,
}))
second = forms.IntegerField(
widget=forms.TextInput(
attrs={
'class': 'number',
'size': 2,
'readonly': True,
'hidden': True,
}))
answer = forms.IntegerField(
widget=forms.TextInput(
attrs={
'type': 'tel',
'class': 'number',
'size': 2,
}))

View File

@ -6,13 +6,6 @@ from django.conf.urls import patterns, url
urlpatterns = patterns(
'',
# Examples:
url(r'^$', 'mmg.views.home', name='home'),
# url(r'^mmg/', include('mmg.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^answer/$', 'mmg.views.answer', name='answer'),
)

View File

@ -1,5 +1,41 @@
import random
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.shortcuts import render
from mmg.forms import NumberForm
MAX = 12
def home(request):
return render(request, 'index.html')
operation = random.choice(['+', '-'])
first = random.choice(range(MAX))
if operation == '-':
if first == 0:
second = 0
else:
second = random.choice(range(first))
else:
second = random.choice(range(MAX))
form = NumberForm({
'first': first,
'operation': operation,
'second': second,
})
return render(
request,
'index.html',
{
'form': form,
}
)
def answer(request):
form = NumberForm(request.POST or None)
print(form)
if form.is_valid():
print(form.cleaned_data)
return HttpResponseRedirect(reverse('mmg.views.home'))

18
static/math.css Normal file
View File

@ -0,0 +1,18 @@
.number {
font-size: 100px;
height: 80px;
font-family: helvetica;
}
.question {
padding-left: 30px;
}
.answer {
padding-top: 30px;
}
#content {
padding-top: 30px;
}

View File

@ -3,20 +3,25 @@
<head>
<title>{{ title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="/static/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<style>
.pw {
padding-top: 100px;
}
</style>
<link href="/static/math.css" rel="stylesheet" media="screen">
</head>
<body>
<section id="content">
hello
<form method="post" action="{% url 'mmg.views.answer' %}">
{% csrf_token %}
<div class="number question">
<span class="number" id="first">{{ form.first.value }}</span>
<span class="number" id="operation">{{ form.operation.value }}</span>
<span class="number" id="second">{{ form.second.value }}</span>
</div>
{{ form.first }}
{{ form.operation }}
{{ form.second }}
{{ form.answer }}
<input type="submit" name="check" value="check"/>
</form>
</section>
<script src="/static/jquery-2.0.0.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/math.js"></script>
</body>
</html>