Fixed URLs of one-sandwich views. Fixed adding sandwiches.
authorKriti Godey <kriti.godey@gmail.com>
Thu, 4 Mar 2010 22:06:09 +0000 (17:06 -0500)
committerKriti Godey <kriti.godey@gmail.com>
Thu, 4 Mar 2010 22:06:09 +0000 (17:06 -0500)
models.py
templates/base.html
templates/ingredient.html
templates/sandwich.html
urls.py
views.py

index abbf825..ed6410f 100644 (file)
--- 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)
index 7b0eb1f..12d9efb 100644 (file)
@@ -21,7 +21,7 @@
                                <h3 class="navtitle">NEWEST</h3>
                                <ul class="newest">
                                        {% for s in sandwiches %}
-                                               <li>{{ s.adjective }}</li>
+                                               <li><a href="{{ s.get_absolute_url }}">{{ s.adjective }}</a></li>
                                        {% endfor %}
                                </ul>
                        </div>
index 04f61bf..23be3a0 100644 (file)
@@ -3,7 +3,7 @@
 {% block title %}Add an ingredient{% endblock %}
 {% block content %}
        <h1 class="pagetitle">Add a new ingredient</h1>
-       <form enctype="multipart/form-data" action="/addingredient/" method="post">
+       <form enctype="multipart/form-data" action="/sandwich/addingredient/" method="post">
        {{ iform.as_p }}
        <input type="submit" value="Submit" />
        </form>
index 1e8e621..d87265e 100644 (file)
@@ -3,7 +3,7 @@
 {% block title %}Add a sandwich{% endblock %}
 {% block content %}
        <h1 class="pagetitle">Add a new sandwich</h1>
-       <form enctype="multipart/form-data" action="/addsandwich/" method="post">
+       <form enctype="multipart/form-data" action="/sandwich/add/" method="post">
        {{ sform.as_p }}
        <input type="submit" value="Submit" />
        </form>
diff --git a/urls.py b/urls.py
index 26ce058..b370aff 100644 (file)
--- 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<slug>[-\w]+)/$', views.specific_sandwich),
        (r'^sandwich/(?P<year>[-\w]+)/(?P<month>[-\w]+)/$', views.sandwich_month),
+       url(r'^sandwich/(?P<slug>[-\w]+)/$', views.specific_sandwich, name='sandwich_by_slug'),
        (r'^login/$', views.login_view),
        (r'^signup/$', views.create_user),
        (r'', views.baseview),
index 2b92417..cb8e7e6 100644 (file)
--- 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,})