how to redirect from HomePage(Page) to your custom app model
Asked Answered
X

1

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.

Xerox answered 13/3, 2018 at 15:31 Comment(0)
I
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

Intoxicant answered 14/3, 2018 at 21:24 Comment(7)
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.Xerox
I have updated my answer. You can redirect by overriding Wagtail's serve() method in your model.Intoxicant
@Xerox did you try it out?Intoxicant
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.Xerox
@Xerox 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.Intoxicant
@Xerox did you try it?Intoxicant
sorry was away for a while, couldn't answer. Tried your way. Worked perfectly fine. Thanks.Xerox

© 2022 - 2024 — McMap. All rights reserved.