From 0ee52e2ec5e267a0c73092202902707176f96e2e Mon Sep 17 00:00:00 2001 From: Kriti Godey Date: Fri, 26 Feb 2010 18:10:52 -0500 Subject: [PATCH] Custom form for adding ingredients works! --- templates/ingredient.html | 11 +++++++++++ urls.py | 1 + views.py | 17 ++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 templates/ingredient.html diff --git a/templates/ingredient.html b/templates/ingredient.html new file mode 100644 index 0000000..6d8b912 --- /dev/null +++ b/templates/ingredient.html @@ -0,0 +1,11 @@ + + + Add an ingredient! + +

Add a new ingredient

+
+ {{ iform.as_p }} + +
+ + \ No newline at end of file diff --git a/urls.py b/urls.py index 82f72f0..c90bca8 100644 --- a/urls.py +++ b/urls.py @@ -4,4 +4,5 @@ from django.shortcuts import render_to_response urlpatterns = patterns('', (r'^addsandwich/$', views.add_sandwich), + (r'^addingredient/$', views.add_ingredient), ) diff --git a/views.py b/views.py index a82b106..49f1c68 100644 --- a/views.py +++ b/views.py @@ -19,4 +19,19 @@ def add_sandwich(request): else: form = SandwichForm() # An unbound form - return render_to_response('sandwich.html', {'sform': form,}) \ No newline at end of file + return render_to_response('sandwich.html', {'sform': form,}) + + +def add_ingredient(request): + if request.method == 'POST': # If the form has been submitted... + form = IngredientForm(request.POST) # A form bound to the POST data + if form.is_valid(): # All validation rules pass + newsandwich = form.save() + newsandwich.save() + thankshtml = "

Thanks! Your ingredient has been saved!

" + return HttpResponse(thankshtml) # Redirect after POST + else: + form = IngredientForm() # An unbound form + + return render_to_response('ingredient.html', {'iform': form,}) + -- 2.20.1