2015-11-19 69 views
3

我正在建立我的第一个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'), 
    ] 
+0

你知道如何将变量传递到url吗?我可以帮助那部分,但我不知道如何提取日期的具体部分,并把它们放在网址中。 – Programmingjoe

+0

这里是文档:https://docs.djangoproject.com/en/1.8/topics/http/urls/ – Programmingjoe

+1

我没有一个完整的解决方案,但我建议看看自定义的'路线'方法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

回答

0

我不知道你是否能鹡鸰整合这一点,但这里是你如何可以使用Django达到一个例子:

  1. 更新你的模型自动生成蛞蝓(:根据标题)在blog/models.py
from django.db import models 
from django.utils.text import slugify 
from django.utils.timezone import now 

class Post(models.Model): 
    # your attributes 
    date = models.DateTimeField(default=now()) 
    title = models.CharField(max_length=250) 
    slug = models.SlugField() 

    def save(self): 
     """ 
     Generate and save slug based on title 
     """ 
     super(Post, self).save() 
     self.slug = slugify(self.title) 
     super(Post, self).save() 
  • 添加一个新的URL在博客应用程序(blog/urls.py):
  • url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/(?P<slug>[\w-]+)$', views.post_details) 
    
  • 定义视图获得那个职位在blog/views.py
  • def post_details(request, year, month, day, slug): 
        post = Post.objects.get(slug=slug, date__year=year, date__month=month, date__day=day) 
        # do what you want with this post 
    
    +0

    在RegEx下面使用单个和两位数的月份和日期值: –

    +0

    url(r'^(?P [0-9] {4} )/(?P [0-9] {1,2})/(?P [0-9] {1,2})/(?P [\ w - ] +)$' ,views .post_details) –

    0

    这里是一个更Wagta实现这个解决方案的具体方法是,假设您还有一个包含所有下面的博客页面的BlogIndexPage。尽管即使您的BlogPages存在于主页下,这也可以工作,只需相应地移动您的路线方法即可。

    概述:

    • 我们将在BlogPageIndex被更新子网址(或任何你的博客网页位于内页)。我们通过添加mixin RoutablePageMixin来做到这一点。
    • 我们就可以使用@route作为装饰过的方法上BlogPageIndex任何种类的自定义视图我们想要的。
    • 最后,我们需要更新我们的BlogPage模型,以便其正常的URL将遵循我们的新URL映射。我们通过覆盖我们的BlogPage上的set_url_path来做到这一点。
    • 这将使用正常slug字段从BlogPage模型和场date_published生成新url_path

    见例如/myapp/models.py

    from django.shortcuts import get_object_or_404 
    
    from wagtail.contrib.routable_page.models import RoutablePageMixin, route 
    from wagtail.wagtailcore.models import Page 
    
    
    class BlogPage(Page): 
        # using date_published instead of date to reduce ambiguity 
        date_published = models.DateField(
         "Date post published", blank=True, null=True 
        ) 
        # ...other fields: main_image, intro, body 
        # ...search_fields 
        # ...content_panels 
    
        # override the set_url_path method to use generate different URL 
        # this is updated on save so each page will need to be re-published 
        # old URL will still work 
        def set_url_path(self, parent): 
         # initially set the attribute self.url_path using the normal operation 
         super().set_url_path(parent=parent) 
         self.url_path = self.url_path.replace(
          self.slug, '{:%Y/%m/%d/}'.format(self.date_published) + self.slug 
         ) 
    
    
    class BlogIndexPage(RoutablePageMixin, Page): 
        # note - must inherit RoutablePageMixin AND Page 
    
        # ...other fields 
        # ...search_fields 
        # ...content_panels 
    
        # route for sub-pages with a date specific URL for posts 
        # this will NOT make a list of pages at blog/2018 just specific blogs only 
        @route(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/(?P<slug>[\w-]+)/?$') 
        def blog_page_by_date(self, request, year, month, day, slug, name='blog-by-date'): 
         """Serve a single blog page at URL (eg. .../2018/01/23/my-title/)""" 
         blog_page = get_object_or_404(
          BlogPage, 
          date_published__year=year, 
          date_published__month=month, 
          date_published__day=day, 
          slug=slug 
         ) 
         return blog_page.serve(request) 
    
        # Speficies that only BlogPage objects can live under this index page 
        subpage_types = ['BlogPage'] 
    

    注意事项:

    • 每个现有的博客页面将需要重新保存触发更新到url_path。
    • 原始的url_path仍然有效,而不是重定向,可以通过覆盖serve方法并检查是否使用旧URL,然后重定向来添加重定向。
    • 这并没有考虑任何SEO的影响,有多个网页的网址。
    相关问题