From: Kriti Godey Date: Fri, 5 Mar 2010 04:53:13 +0000 (-0500) Subject: Made the user field on the sandwich auto fill in and removed it from the displayed... X-Git-Url: http://git.ithinksw.org/~kgodey/maayanwich.git/commitdiff_plain/22c8f72c1e6142fcf857bd24711ea89199075658 Made the user field on the sandwich auto fill in and removed it from the displayed form. --- 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.

"