<s> from django . core . urlresolvers import reverse <EOL> from django . http import Http404 , HttpResponsePermanentRedirect <EOL> from django . shortcuts import get_object_or_404 , render <EOL> from fruit . models import Kind <EOL> from . models import Herbarium <EOL> def index ( request , cls = None , slug = None ) : <EOL> context = { <EOL> '<STR_LIT>' : Kind . CLS . choices , <EOL> } <EOL> herbs = Herbarium . objects . select_related ( '<STR_LIT>' ) <EOL> if cls : <EOL> cls_ids = [ i for i , name in Kind . CLS . choices ] <EOL> cls = int ( cls ) <EOL> if cls not in cls_ids : <EOL> raise Http404 <EOL> else : <EOL> cls_slug = Kind . cls_slug ( cls ) <EOL> if cls_slug != slug : <EOL> return HttpResponsePermanentRedirect ( <EOL> reverse ( '<STR_LIT>' , args = [ cls , cls_slug ] ) <EOL> ) <EOL> herbs = herbs . filter ( kind__cls = cls ) <EOL> context [ '<STR_LIT>' ] = cls <EOL> context [ '<STR_LIT:index>' ] = herbs <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> def detail ( request , pk , slug = None ) : <EOL> item = get_object_or_404 ( Herbarium . objects . select_related ( '<STR_LIT>' ) , pk = pk ) <EOL> if item . slug != slug : <EOL> return HttpResponsePermanentRedirect ( item . get_absolute_url ( ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : item } ) </s>
