From d70badd3a28a3b951b32e4cf36562ec3cfd9dd41 Mon Sep 17 00:00:00 2001 From: Kriti Godey Date: Thu, 4 Mar 2010 17:06:09 -0500 Subject: [PATCH] Fixed URLs of one-sandwich views. Fixed adding sandwiches. --- models.py | 12 ++++++++++++ templates/base.html | 2 +- templates/ingredient.html | 2 +- templates/sandwich.html | 2 +- urls.py | 2 +- views.py | 9 +++++---- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/models.py b/models.py index abbf825..ed6410f 100644 --- a/models.py +++ b/models.py @@ -1,6 +1,10 @@ from django.db import models from django.contrib.auth.models import User import datetime +from django.core.urlresolvers import reverse +from django.template.defaultfilters import slugify + + class Ingredient(models.Model): @@ -10,6 +14,7 @@ class Ingredient(models.Model): def __unicode__(self): return self.name + class Sandwich(models.Model): adjective = models.CharField(max_length=200) slug = models.SlugField() @@ -26,3 +31,10 @@ class Sandwich(models.Model): def __unicode__(self): return self.adjective + + def get_absolute_url(self): + return reverse('sandwich_by_slug', kwargs={'slug':self.slug}) + + def save(self, *args, **kwargs): + self.slug = slugify(self.adjective) + super(Sandwich, self).save(*args, **kwargs) diff --git a/templates/base.html b/templates/base.html index 7b0eb1f..12d9efb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -21,7 +21,7 @@ diff --git a/templates/ingredient.html b/templates/ingredient.html index 04f61bf..23be3a0 100644 --- a/templates/ingredient.html +++ b/templates/ingredient.html @@ -3,7 +3,7 @@ {% block title %}Add an ingredient{% endblock %} {% block content %}

Add a new ingredient

-
+ {{ iform.as_p }}
diff --git a/templates/sandwich.html b/templates/sandwich.html index 1e8e621..d87265e 100644 --- a/templates/sandwich.html +++ b/templates/sandwich.html @@ -3,7 +3,7 @@ {% block title %}Add a sandwich{% endblock %} {% block content %}

Add a new sandwich

-
+ {{ sform.as_p }}
diff --git a/urls.py b/urls.py index 26ce058..b370aff 100644 --- a/urls.py +++ b/urls.py @@ -6,8 +6,8 @@ urlpatterns = patterns('', (r'^sandwich/add/$', views.add_sandwich), (r'^sandwich/addingredient/$', views.add_ingredient), (r'^sandwich/all/$', views.all_sandwich), - (r'^sandwich/(?P[-\w]+)/$', views.specific_sandwich), (r'^sandwich/(?P[-\w]+)/(?P[-\w]+)/$', views.sandwich_month), + url(r'^sandwich/(?P[-\w]+)/$', views.specific_sandwich, name='sandwich_by_slug'), (r'^login/$', views.login_view), (r'^signup/$', views.create_user), (r'', views.baseview), diff --git a/views.py b/views.py index 2b92417..cb8e7e6 100644 --- a/views.py +++ b/views.py @@ -11,10 +11,6 @@ import datetime def add_sandwich(request): - if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] - else: - sandwiches = Sandwich.objects.order_by('date_made') 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 @@ -25,6 +21,11 @@ def add_sandwich(request): else: form = SandwichForm() # An unbound form + if Sandwich.objects.count() > 5: + sandwiches = Sandwich.objects.order_by('date_made')[:5] + else: + sandwiches = Sandwich.objects.order_by('date_made') + return render_to_response('sandwich.html', {'sform': form, 'sandwiches': sandwiches,}) -- 2.20.1