Django URL 配置

发布于 2024-08-20 12:47:57 字数 2182 浏览 5 评论 0原文

假设我有 3 个模型:城市、区域、条目。

每个城市都有多个区域,每个区域可以有多个条目,但对于“现在”来说,只能有一个活动条目并且它将被显示。所以在逻辑上:

请注意,每个城市、区域、条目将使用相关模型类的 slug 变量

格式将如下:

www.mysite.com/<slug of city>/<slug of area>/<slug of entry>

www.mysite.com/mycity/myarea/ ->将显示绑定到该区域并且处于活动状态的条目(这可以通过使用区域的 active_entry 函数来检测)。

但用户可以查看一些旧条目,例如:

www.mysite.com/mycity/myarea/some-old-entry-that-is-no-longer-active

我已经通过阅读编写了 get_absolute_url 函数“实用 Django 项目第二版”书,但现在我陷入困境。

我有这样的模型:

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

请帮助这个可怜的灵魂配置他的 url 配置。

谢谢

Assume I have 3 Models: City, Area, Entry.

Each city has several Areas and each area can have several entries BUT for "now", there can be will be only one active Entry and it will be shown. So in logic:

Note that each city, area, entry will be using slug variable of related model class

Format will be in such:

www.mysite.com/<slug of city>/<slug of area>/<slug of entry>

www.mysite.com/mycity/myarea/ -> will be displaying an Entry that is bound to that Area AND active (this can be detected by using Area's active_entry function).

But users can view some old Entries such as:

www.mysite.com/mycity/myarea/some-old-entry-that-is-no-longer-active

I have written get_absolute_url functions by reading the "Practical Django Projects 2nd Edition" book but now I am stucked.

I have such models:

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

Please help this poor soul to configure his url configuration.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2024-08-27 12:47:57

它可能看起来像这样:

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'),
)

It should probably look like something like that:

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'),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文