Skip to content

Commit

Permalink
Example of how to use two slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Aug 28, 2017
1 parent e63ac29 commit dfc1c11
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
7 changes: 6 additions & 1 deletion example/date_views/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from django.contrib import admin
from .models import Article
from .models import Article, Dateline


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
pass


@admin.register(Dateline)
class DatelineAdmin(admin.ModelAdmin):
pass
36 changes: 36 additions & 0 deletions example/date_views/migrations/0002_auto_20170828_1921.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-08-28 19:21
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('date_views', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Dateline',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('city', models.CharField(max_length=500)),
('state', models.CharField(max_length=500)),
('city_slug', models.SlugField(max_length=500)),
('state_slug', models.SlugField(max_length=500)),
],
),
migrations.AlterUniqueTogether(
name='dateline',
unique_together=set([('city_slug', 'state_slug')]),
),
migrations.AddField(
model_name='article',
name='dateline',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='date_views.Dateline'),
preserve_default=False,
),
]
21 changes: 21 additions & 0 deletions example/date_views/migrations/0003_auto_20170828_1921.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-08-28 19:21
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('date_views', '0002_auto_20170828_1921'),
]

operations = [
migrations.AlterField(
model_name='article',
name='dateline',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='date_views.Dateline'),
),
]
22 changes: 22 additions & 0 deletions example/date_views/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,31 @@
from django.db import models
from django.core.urlresolvers import reverse


class Article(models.Model):
"""
A story.
"""
title = models.CharField(max_length=200)
pub_date = models.DateField()
dateline = models.ForeignKey('Dateline', null=True)

def get_absolute_url(self):
return reverse('article-detail', kwargs={'pk': self.pk})


class Dateline(models.Model):
"""
The location where a story was filed.
"""
city = models.CharField(max_length=500)
state = models.CharField(max_length=500)

city_slug = models.SlugField(max_length=500)
state_slug = models.SlugField(max_length=500)

class Meta:
unique_together = ("city_slug", "state_slug")

def get_absolute_url(self):
return reverse('dateline-detail', kwargs={'city_slug': self.city_slug, 'state_slug': self.state_slug})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ object }}
15 changes: 13 additions & 2 deletions example/date_views/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from date_views.models import Article
from date_views.models import Article, Dateline
from bakery.views import (
BuildableArchiveIndexView,
BuildableYearArchiveView,
BuildableMonthArchiveView,
BuildableDayArchiveView
BuildableDayArchiveView,
BuildableDetailView,
)


Expand Down Expand Up @@ -34,3 +35,13 @@ class MyDayArchiveView(BuildableDayArchiveView):
date_field = "pub_date"
month_format = "%m"
#allow_future = True


class MyDetailView(BuildableDetailView):
queryset = Dateline.objects.all()

def get_object(self, queryset=None):
return self.queryset.get(
state_slug=self.kwargs['state_slug'],
city_slug=self.kwargs['city_slug']
)
8 changes: 7 additions & 1 deletion example/project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
MyIndexView,
MyYearArchiveView,
MyMonthArchiveView,
MyDayArchiveView
MyDayArchiveView,
MyDetailView
)


urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(
r'^dateline/(?P<state_slug>[-\w]+)/(?P<city_slug>[-\w]+)/$',
MyDetailView.as_view(),
name="dateline-detail"
),
url(r'^archive/$', MyIndexView.as_view()),
url(r'^archive/(?P<year>[0-9]{4})/$', MyYearArchiveView.as_view()),
url(
Expand Down

0 comments on commit dfc1c11

Please sign in to comment.