English 中文(简体)
Django urls/slugs@#!
原标题:Django urls/slugs@#!

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)),
)

我不认为为什么轮.不行。 也许有人会把我指向正确的方向?

最佳回答

尽管你不愿意提供劳教不工作的任何例子,但问题可能出现在第一大公国。 由于你用<代码><>$ 终止了该编号,以表明该斜线为端,它将与从<代码>home/开始的所有URLs相匹配。

而是:

(r ^home/$ ,  home.views.index ),
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签