Made the user field on the sandwich auto fill in and removed it from the displayed...
authorKriti Godey <kriti.godey@gmail.com>
Fri, 5 Mar 2010 04:53:13 +0000 (23:53 -0500)
committerKriti Godey <kriti.godey@gmail.com>
Fri, 5 Mar 2010 04:53:13 +0000 (23:53 -0500)
forms.py
views.py

index 380f6a4..a2c5fc8 100644 (file)
--- 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):
index 2a175af..9e1ee35 100644 (file)
--- 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 = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
                                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 = "<p class=\"formthanks\">You are not logged in.</p>"