0

スラッグを使用して URL を参照したい。それにもかかわらず、私が使用したいモデルは中国語の語彙クイズのデザインであり、URL を参照するために 3 つの異なるスラッグを使用したいと考えています。

次に、私はモデルを持っています

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

さらに、私はこのurlファイルを持っています

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 にアクセスできます

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

そして他の2つは、次のエラーを受け取りました

指定されたクエリに一致する語彙はありません。

Django はモデルに対して 1 つのスラッグ フィールドのみをサポートしていますか?

Django が複数のスラッグ フィールドをサポートしている場合、何が欠けていますか?

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 に答える