I have a genre category and a sub genre category and I m trying to use dynamic routing, here is my code:
模式:
from django.db import models
from django.db.models import permalink
class Genre(models.Model):
name = models.CharField(max_length=300)
slug = models.SlugField(max_length=150)
def __unicode__(self):
return u %s % self.name
@models.permalink
def get_absolute_url(self):
return ( view_genre , (), { genre_slug : self.slug })
class SubGenre(models.Model):
genre = models.ForeignKey(Genre)
name = models.CharField(max_length=300)
slug = models.SlugField(max_length=150)
def __unicode__(self):
return u %s % self.name
@models.permalink
def get_absolute_url(self):
return ( view_subgenre , (), { subgenre_slug : self.slug })
我的看法:
from django.template import Context, loader, RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.core import serializers
def index(request):
return render_to_response("home.html", context_instance=RequestContext(request),)
def genre(request, slug):
genre = get_object_or_404(Genre, slug=genre_slug)
return render_to_response("genre.html", { subgenre : SubGenre.objects.filter(genre=genre)}, context_instance=RequestContext(request),)
以及我的支持者:
from django.conf.urls.defaults import *
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns( ,
# Example:
(r ^home/ , home.views.index ),
url(r ^home/(?P<genre_slug>[-w]+)/$ , home.views.genre , name= view_genre ),
(r ^static/(?P<path>.*)$ , django.views.static.serve , { document_root : settings.MEDIA_ROOT}),
# Uncomment the admin/doc line below to enable admin documentation:
# (r ^admin/doc/ , include( django.contrib.admindocs.urls )),
# Uncomment the next line to enable the admin:
(r ^admin/ , include(admin.site.urls)),
)
我不认为为什么轮.不行。 也许有人会把我指向正确的方向?