假设我有3个模型:城市,区域,条目。
每个城市都有几个区域,每个区域可以有几个条目但是“现在”,只有一个活动条目,它将被显示。所以在逻辑上:
请注意,每个城市,区域,条目都将使用相关模型类的slug变量
格式将如下:
www.mysite.com/<slug of city>/<slug of area>/<slug of entry>
www.mysite.com/mycity/myarea/
- &gt;将显示绑定到该区域并且处于活动状态的条目(可以使用区域的active_entry
功能检测到这一点。)
但是用户可以查看一些旧的条目,例如:
www.mysite.com/mycity/myarea/some-old-entry-that-is-no-longer-active
我已经通过阅读“Practical Django Projects 2nd Edition”一书编写了get_absolute_url函数,但现在我被困了。
我有这样的模特:
from django.db import models
class Entry(models.Model):
area = models.ForeignKey('Area',verbose_name="The area that this entry belongs to")
slug = slug = models.SlugField(unique=True) # this will be auto populated via admin panel, from title
title = baslik = models.CharField()
content = models.TextField()
start_time = models.DateTimeField()#start time for this entry.
end_time = models.DateTimeField()#end time for this entry.
@models.permalink
def get_absolute_url(self):
return ("entry.detail",(),{"city":self.area.city.slug,"area":self.area.slug,"entry":self.slug})
class Area(models.Model):
city = models.ForeignKey(verbose_name="city that this area belongs to")
name = models.CharField(max_length=30)
slug = models.SlugField(unique=True)# this will be auto populated via admin panel, from name
@models.permalink
def get_absolute_url(self):
return ("bolge.detay",(),{"city":self.city.slug,"area":self.slug})
def active_entry(self):
from datetime import datetime, date, time
now = datetime.now()
try:
return Entry.objects.get(area__exact=self,start_time__lte=now,end_time__gte=now)
except Entry.DoesNotExist:
return False
class City(models.Model):
name =models.CharField(max_length=30)
slug = models.SlugField(unique=True) # this will be auto populated via admin panel, from name
@models.permalink
def get_absolute_url(self):
return ("city.detail",(),{"city":self.slug})
请帮助这个可怜的灵魂配置他的网址配置。
由于
答案 0 :(得分:1)
应该看起来像这样:
urlpatterns = patterns('',
(r'^(?P<city>[a-z-]+)/(?P<area>[a-z-]+)/$', 'yourapp.views.areaview'),
(r'^(?P<city>[a-z-]+)/(?P<area>[a-z-]+)/(?P<entry>[a-z-]+)/$', 'yourapp.views.entryview'),
)