From 22c8f72c1e6142fcf857bd24711ea89199075658 Mon Sep 17 00:00:00 2001 From: Kriti Godey Date: Thu, 4 Mar 2010 23:53:13 -0500 Subject: [PATCH] Made the user field on the sandwich auto fill in and removed it from the displayed form. --- forms.py | 2 +- views.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/forms.py b/forms.py index 380f6a4..a2c5fc8 100644 --- a/forms.py +++ b/forms.py @@ -8,7 +8,7 @@ class SandwichForm(ModelForm): class Meta: model = Sandwich - exclude = ('slug',) + exclude = ('slug', 'user') class IngredientForm(ModelForm): diff --git a/views.py b/views.py index 2a175af..9e1ee35 100644 --- a/views.py +++ b/views.py @@ -18,12 +18,13 @@ def add_sandwich(request): if request.method == 'POST': # If the form has been submitted... form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data if form.is_valid(): # All validation rules pass - newsandwich = form.save() + newsandwich = form.save(commit=False) + newsandwich.user = request.user newsandwich.save() thankshtml = "

Thanks! Your sandwich has been added!

" return HttpResponse(thankshtml) # Redirect after POST else: - form = SandwichForm() # An unbound form + form = SandwichForm(initial={'user': request.user}) # An unbound form return render_to_response('sandwich.html', {'sform': form, 'sandwiches': sandwiches, 'user': request.user,}) else: thankshtml = "

You are not logged in.

" -- 2.20.1