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):
def __unicode__(self):
return self.name
+
class Sandwich(models.Model):
adjective = models.CharField(max_length=200)
slug = models.SlugField()
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)
<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>
{% 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>
{% 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>
(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),
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
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,})