5

I've created an app "Blog". In my app I've got several models including "BlogIndex(Page)". When I run local server I find myself at "home_page.html". What I want is to start my local server at "blog_index.html". I know that I can set a root page at settings>site>localhost to make my "blog_index.html" a root page, but I can't do this because in my app I've got some other models that live at the same level as "BlogIndex(Page)" and they are children of the root which is "HomePage" so it would brake my code. So my question is: can I make a redirect from "HomePage(Page)" to my "BlogIndex" so that when i start my server I would be automatically redirected from "HomePage" to "BlogIndex"? How can I do it? How much it will affect the performance of the site and it's optimization?

I know that there is settings>redirect but it works only for inactive pages, but i need "HomePage" to be active. Thank you.

madjack99
  • 103
  • 8

1 Answers1

7

Perhaps a better approach would be to display your blog posts (and any other models you want) on your homepage. Just override get_context(). See here: Wagtail Views: extra context

Update: You can redirect by overriding the serve() method. For example, in your model, you would do something like:

# home/models.py
...
from django.http import HttpResponseRedirect
from django.urls import reverse

class HomePage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),
    ]

    def serve(self, request):
        # Redirect to blog index page
        return HttpResponseRedirect('/blog/')

        # only do this if you're using urls.py and namespaces
        # return HttpResponseRedirect(reverse('blog:index'))

More info: http://docs.wagtail.io/en/latest/reference/pages/model_recipes.html?highlight=serve()#overriding-the-serve-method

sooz
  • 96
  • 3
  • thanks for reply. It might be a way out but I thought that there must be some "wagtail" solution for that issue, that I haven't found. – madjack99 Mar 15 '18 at 08:42
  • I have updated my answer. You can redirect by overriding Wagtail's serve() method in your model. – sooz Mar 15 '18 at 15:37
  • @madjack99 did you try it out? – sooz Mar 17 '18 at 17:08
  • No, no it didn't work. As far as I understand `reverse('blog:index'))` contraction may be used only in case of named urls. I'm not sure how are urls are named in wagtail and how to get these names. – madjack99 Mar 18 '18 at 16:00
  • @madjack99 I have updated the code with the url that you need for your use case. Since your'e not using a urls.py, you can simply do `return HttpResponseRedirect('/blog/')`. I have tested this code and it works. Of course, I'm assuming that your Wagtail blog index page is in fact at the url path /blog/. If it's not, just use whatever path you have that set to. It's up to you. – sooz Mar 18 '18 at 22:33
  • @madjack99 did you try it? – sooz Mar 20 '18 at 20:35
  • sorry was away for a while, couldn't answer. Tried your way. Worked perfectly fine. Thanks. – madjack99 Mar 27 '18 at 13:16