0

我想使用 slug 来引用 url。尽管如此,我想使用的模型是为中文词汇测验设计的,我想使用 3 种不同的 slug 来引用 url。

然后,我有一个模型

模型.py

from django.db import models
from django.db.models import permalink
from django.template.defaultfilters import slugify
from unidecode import unidecode

class Vocabulary( models.Model ):

title = models.CharField(" Title ",  max_length = 100 )
vocabularyCharacter = models.CharField("Character",  max_length = 100, blank = True, null = True )
vocabularyTranslation = models.CharField("Translation",  max_length = 100, blank = True, null = True )
vocabularyPinyin = models.CharField("Pinyin",  max_length = 100, blank = True, null = True )
vocabularyCharacterSlug = models.SlugField( "Character Slug (optional)",  max_length = 100, blank = True, null = True )
vocabularyTranslationSlug = models.SlugField("Translation Slug (optional)",  max_length = 100, blank = True, null = True )
vocabularyPinyinSlug = models.SlugField("Pinyin Slug (optional)",  max_length = 100, blank = True, null = True )
audio_file = models.FileField("Audio file (optional)", upload_to = u'resources/mp3/', max_length=200, blank = True, null=True)
description = models.TextField("Description ",  blank = True, null = True )

class Meta:
    verbose_name = 'vocabulary'
    verbose_name_plural = 'vocabularies'

def __unicode__( self ):
    return u"%s" % self.title

def save( self, force_insert = False, force_update = False ):
    self.vocabularyCharacterSlug = slugify(unidecode(self.vocabularyCharacter))
    self.vocabularyTranslationSlug = slugify(unidecode(self.vocabularyTranslation))
    self.vocabularyPinyinSlug = slugify(unidecode(self.vocabularyPinyin))
    super( Vocabulary, self ).save( force_insert, force_update )

@permalink
def get_absolute_vocabulary_character_url( self ):
    return ( 'vocabulary_character_detail', None, {
        'vocabularyCharacterSlug':    self.vocabularyCharacterSlug,
    } )

@permalink
def get_absolute_vocabulary_traslation_url( self ):
    return ( 'vocabulary_traslation_detail', None, {
        'vocabularyTranslationSlug':    self.vocabularyTranslationSlug,
    } )

@permalink
def get_absolute_vocabulary_pinyin_url( self ):
    return ( 'vocabulary_pinyin_detail', None, {
        'vocabularyPinyinSlug':    self.vocabularyPinyinSlug,
    } )

然后,我对这个模型有一个看法

视图.py

from django.shortcuts import get_object_or_404, render_to_response
from core.vocabulary.models import Vocabulary

def vocabulary_character_detail( request, vocabularyCharacterSlug ):

    vocabulary = get_object_or_404( Vocabulary, vocabularyCharacterSlug = vocabularyCharacterSlug )
    return render_to_response( 'vocabulary/vocabulary_character_detail.html',
                              {'vocabulary': vocabulary} )

def vocabulary_translation_detail( request, vocabularyTranslationSlug ):

    vocabulary = get_object_or_404( Vocabulary, vocabularyTranslationSlug = vocabularyTranslationSlug )
    return render_to_response( 'vocabulary/vocabulary_translation_detail.html',
                              {'vocabulary': vocabulary} )

def vocabulary_pinyin_detail( request, vocabularyPinyinSlug ):

    vocabulary = get_object_or_404( Vocabulary, vocabularyPinyinSlug = vocabularyPinyinSlug )
    return render_to_response( 'vocabulary/vocabulary_pinyin_detail.html',
                              {'vocabulary': vocabulary} )

另外,我有这个 url 文件

网址.py

from django.conf.urls import patterns, url
from core.vocabulary.views.vocabulary import vocabulary_character_detail
from core.vocabulary.views.vocabulary import vocabulary_translation_detail, vocabulary_pinyin_detail

urlpatterns = patterns( '',     
        url( r'^(?P<vocabularyCharacterSlug>[-\w]+)/$',
            vocabulary_character_detail,
            name = 'vocabulary_character' ),

        url( r'^(?P<vocabularyTranslationSlug>[-\w]+)/$',
            vocabulary_translation_detail,
            name = 'vocabulary_translation' ),

        url( r'^(?P<vocabularyPinyinSlug>[-\w]+)/$',
            vocabulary_pinyin_detail,
            name = 'vocabulary_pinyin' ),
 )

我创建了相应的模板,我可以访问 url

url( r'^(?P<vocabularyCharacterSlug>[-\w]+)/$',
            vocabulary_character_detail,
            name = 'vocabulary_character' ),

而另外2个,我收到一条错误消息

没有词汇与给定的查询匹配。

Django 是否只支持模型的一个 slug 字段?

如果 Django 支持多个 slug 字段,我错过了什么?

4

1 回答 1

0

为什么不将您的视图指向不同的目录?

urlpatterns = patterns( '',     
    url( r'^character/(?P<vocabularyCharacterSlug>[-\w]+)/$',
        vocabulary_character_detail,
        name = 'vocabulary_character' ),

    url( r'^translation/(?P<vocabularyTranslationSlug>[-\w]+)/$',
        vocabulary_translation_detail,
        name = 'vocabulary_translation' ),

    url( r'^pinyin/(?P<vocabularyPinyinSlug>[-\w]+)/$',
        vocabulary_pinyin_detail,
        name = 'vocabulary_pinyin' ),

)

模型:

def vocabulary_character_detail( request, vocabularyCharacterSlug ):

   vocabulary = get_object_or_404( Vocabulary, vocabularyCharacterSlug = vocabularyCharacterSlug )
   return render_to_response( 'vocabulary/character/vocabulary_character_detail.html',
                          {'vocabulary': vocabulary} )

def vocabulary_translation_detail( request, vocabularyTranslationSlug ):

   vocabulary = get_object_or_404( Vocabulary, vocabularyTranslationSlug = vocabularyTranslationSlug )
   return render_to_response( 'vocabulary/translation/vocabulary_translation_detail.html',
                          {'vocabulary': vocabulary} )

def vocabulary_pinyin_detail( request, vocabularyPinyinSlug ):

   vocabulary = get_object_or_404( Vocabulary, vocabularyPinyinSlug = vocabularyPinyinSlug )
   return render_to_response( 'vocabulary/pinyin/vocabulary_pinyin_detail.html',
                          {'vocabulary': vocabulary} 
于 2012-08-07T11:03:43.273 回答