我可以在Django上为模型使用多个slug字段吗?

时间:2012-08-07 09:28:54

标签: python django url web-applications slug

我想使用slug来引用网址。尽管如此,我想要使用的模型是设计中文词汇测验,我想使用3个不同的slu to来引用网址。

然后,我有一个模型

models.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,
    } )

然后,我有了这个模型的视图

view.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} )

此外,我有这个网址文件

urls.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( r'^(?P<vocabularyCharacterSlug>[-\w]+)/$',
            vocabulary_character_detail,
            name = 'vocabulary_character' ),

另外2,我收到错误说

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

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

如果Django支持多个slug字段,我缺少什么?

1 个答案:

答案 0 :(得分: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}