1 from django.http import HttpResponse
2 from forms import SandwichForm, IngredientForm
3 from django.shortcuts import render_to_response
4 from django.core.files.uploadedfile import SimpleUploadedFile
5 from models import Sandwich, Ingredient
6 from django.http import Http404
10 def add_sandwich(request):
11 if request.method == 'POST': # If the form has been submitted...
12 form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data
13 if form.is_valid(): # All validation rules pass
14 newsandwich = form.save()
16 thankshtml = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
17 return HttpResponse(thankshtml) # Redirect after POST
19 form = SandwichForm() # An unbound form
21 return render_to_response('sandwich.html', {'sform': form,})
24 def add_ingredient(request):
25 if request.method == 'POST': # If the form has been submitted...
26 form = IngredientForm(request.POST) # A form bound to the POST data
27 if form.is_valid(): # All validation rules pass
28 newsandwich = form.save()
30 thankshtml = "<p class=\"formthanks\">Thanks! Your ingredient has been saved!</p>"
31 return HttpResponse(thankshtml) # Redirect after POST
33 form = IngredientForm() # An unbound form
35 return render_to_response('ingredient.html', {'iform': form,})
38 def all_sandwich(request):
40 sandwiches = Sandwich.objects.all()
41 except Sandwich.DoesNotExist:
43 return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
46 def newsandwiches(request):
48 if Sandwich.objects.count() > 5:
49 sandwiches = Sandwich.objects.order_by('date_made')[:5]
51 sandwiches = Sandwich.objects.order_by('date_made')
52 except Sandwich.DoesNotExist:
54 return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
57 def specific_sandwich(request, slug):
59 sandwiches = Sandwich.objects.get(slug=slug)
60 except Sandwich.DoesNotExist:
62 return render_to_response('onesandwich.html', {'s': sandwiches,})