我正在建立我的第一个w d django网站。我的网站有一个博客部分,我想在网址中添加发布日期。目前,添加页面时,URL变为:example.com/blog/[slug],但我希望它是:example.com/blog/2015/11/19/[slug]添加日期到博客页面的网址
我的博客页面:
class BlogPage(Page):
main_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
search_fields = Page.search_fields + (
index.SearchField('intro'),
index.SearchField('body'),
)
content_panels = Page.content_panels + [
FieldPanel('date'),
ImageChooserPanel('main_image'),
FieldPanel('intro'),
FieldPanel('body'),
]
你知道如何将变量传递到url吗?我可以帮助那部分,但我不知道如何提取日期的具体部分,并把它们放在网址中。 – Programmingjoe
这里是文档:https://docs.djangoproject.com/en/1.8/topics/http/urls/ – Programmingjoe
我没有一个完整的解决方案,但我建议看看自定义的'路线'方法http://docs.wagtail.io/en/v1.2/reference/pages/model_recipes.html#adding-endpoints-with-custom-route-methods,或者可能用'RoutablePageMixin'做些事情:http:// docs .wagtail.io/en/v1.2/reference/contrib/routablepage.html – gasman