Skip to content

Commit ad700fb

Browse files
Matthew HolmesMatthew Holmes
authored andcommitted
adding finished django Lab 1
1 parent 14c8710 commit ad700fb

File tree

4 files changed

+103
-11
lines changed

4 files changed

+103
-11
lines changed

Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/index.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<body>
1010
<h1>Unit Converter</h1>
1111

12-
<form action="" method="post">
12+
<form action="ucp/result/" method="post">
1313
{% csrf_token %}
1414
<!-- -->
1515

@@ -28,26 +28,26 @@ <h1>Unit Converter</h1>
2828
<h3>Enter the units you are using</h3>
2929

3030
<label for="km">Kilometers:</label>
31-
<input type="radio" name="user_units" id="" />
31+
<input type="radio" name="user_units" id="" value="km" />
3232

3333
<label for="m">Meters:</label>
34-
<input type="radio" name="user_units" id="" />
34+
<input type="radio" name="user_units" id="" value="m" />
3535

3636
<label for="ft">Feet:</label>
37-
<input type="radio" name="user_units" id="" />
37+
<input type="radio" name="user_units" id="" value="ft" />
3838
</div>
3939

4040
<div class="conversion_units">
4141
<h3>Enter the units you wish to convert to</h3>
4242

4343
<label for="km">Kilometers:</label>
44-
<input type="radio" name="conversion_units" id="" />
44+
<input type="radio" name="conversion_units" id="" value="km" />
4545

4646
<label for="m">Meters:</label>
47-
<input type="radio" name="conversion_units" id="" />
47+
<input type="radio" name="conversion_units" id="" value="m" />
4848

4949
<label for="ft">Feet:</label>
50-
<input type="radio" name="conversion_units" id="" />
50+
<input type="radio" name="conversion_units" id="" value="ft" />
5151
</div>
5252

5353
<button type="submit">submit</button>

Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/result.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
<body>
1010
<h1>Results</h1>
1111

12-
{{% distance %}}
12+
<p>
13+
You converted {{distance}}{{units}} into
14+
{{converted_output}}{{units_to_convert}}
15+
</p>
16+
17+
distance {{ distance }} units {{units}} converted units {{converted_output}}
18+
units to convert {{units_to_convert}}
1319
</body>
1420
</html>

Code/matthew/django/labs/01_django_redo/unit_converter_app/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
path('', views.index, name='index'),
1111

1212

13-
path('result/', views.forms, name='forms')
13+
path('/result/', views.forms, name='forms')
1414

1515
# 8000/rps/result
1616
# path('result/', views.result, name='result'),

Code/matthew/django/labs/01_django_redo/unit_converter_app/views.py

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
from django.shortcuts import render
23

34
# Create your views here.
@@ -11,5 +12,90 @@ def index(request):
1112
# request.POST is grabbing the information inside of the from submission
1213
def forms(request):
1314
form_post = request.POST
14-
# print(form_post)
15-
return (request, 'unit_converter_app/result.html', form_post)
15+
16+
#EXAMPLE: distance=form_post['distance']
17+
18+
distance = form_post['distance']
19+
units = form_post['user_units']
20+
units_to_convert = form_post['conversion_units']
21+
22+
def feet_to_meters(x):
23+
converted_ft = float(x) * .3048
24+
return converted_ft
25+
26+
def mile_to_meters(x):
27+
converted_mi = float(x) * 1609.34
28+
return converted_mi
29+
30+
def km_to_meters(x):
31+
converted_km = float(x) * 1000
32+
return converted_km
33+
34+
def meters_to_feet(x):
35+
converted_ft = float(x) * 3.28084
36+
return converted_ft
37+
38+
def meters_to_mile(x):
39+
converted_mi = float(x) * .000621371
40+
return converted_mi
41+
42+
def meters_to_km(x):
43+
converted_km = float(x) * .001
44+
return converted_km
45+
46+
if units.lower() == 'mi' and units_to_convert.lower() == 'km':
47+
converted_output = meters_to_km(mile_to_meters(distance))
48+
print(f'\n {distance}mi is {converted_output}km')
49+
50+
elif units.lower() == 'mi' and units_to_convert.lower() == 'm':
51+
converted_output = mile_to_meters(distance)
52+
print(f'\n {distance}mi is {converted_output}m')
53+
54+
elif units.lower() == 'mi' and units_to_convert.lower() == 'ft':
55+
converted_output = meters_to_feet(mile_to_meters(distance))
56+
print(f'\n {distance}mi is {converted_output}ft')
57+
58+
elif units.lower() == 'km' and units_to_convert.lower() == 'mi':
59+
converted_output = meters_to_mile(km_to_meters(distance))
60+
print(f'\n {distance}km is {converted_output}mi')
61+
62+
elif units.lower() == 'km' and units_to_convert.lower() == 'm':
63+
converted_output = km_to_meters(distance)
64+
print(f'\n {distance}km is {converted_output}m')
65+
66+
elif units.lower() == 'km' and units_to_convert.lower() == 'ft':
67+
converted_output = meters_to_feet(km_to_meters(distance))
68+
print(f'\n {distance}km is {converted_output}ft')
69+
70+
elif units.lower() == 'ft' and units_to_convert.lower() == 'mi':
71+
converted_output = mile_to_meters(feet_to_meters(distance))
72+
print(f'\n {distance}ft is {converted_output}mi')
73+
74+
elif units.lower() == 'ft' and units_to_convert.lower() == 'm':
75+
converted_output = feet_to_meters(distance)
76+
print(f'\n {distance}ft is {converted_output}m')
77+
78+
elif units.lower() == 'ft' and units_to_convert.lower() == 'km':
79+
converted_output = meters_to_km(feet_to_meters(distance))
80+
print(f'\n {distance}mi is {converted_output}ft')
81+
82+
elif units.lower() == 'm' and units_to_convert.lower() == 'ft':
83+
converted_output = meters_to_feet(distance)
84+
print(f'\n {distance}km is {converted_output}ft')
85+
86+
elif units.lower() == 'm' and units_to_convert.lower() == 'mi':
87+
converted_output = meters_to_mile(distance)
88+
print(f'\n {distance}ft is {converted_output}mi')
89+
90+
elif units.lower() == 'm' and units_to_convert.lower() == 'km':
91+
converted_output = meters_to_km
92+
print(f'\n {distance}mi is {converted_output}ft')
93+
94+
context = {
95+
'distance': distance,
96+
'converted_output': converted_output,
97+
'units': units,
98+
'units_to_convert': units_to_convert,
99+
}
100+
101+
return render(request, 'unit_converter_app/result.html', context)

0 commit comments

Comments
 (0)