2015-11-19 72 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的影響,有多個網頁的網址。
    相關問題