Omar Gonzales
Omar Gonzales

Reputation: 4008

How to set the URL for View using Wagtail?

Does Wagtail only use the slug in Promote Tab, or should I set the url in urls.py?

I'm using Wagtail, I've 2 pages:

a) Ministerios Internacionales Elim
b) Doctrina Elim

I want to set a as homepage, and b to:

http://127.0.0.1:8000/doctrina-elim/

enter image description here

Right now doctrina/urls.py is empty :(

I've this model in doctrina/models.py:

class DoctrinaIndexPage(Page):

    template = 'doctrina/doctrina_index_page.html'

    nivel = models.CharField(max_length=255, default='Básico')

    subtitle = models.CharField(max_length=255, default='Publicación reciente')

    body = RichTextField(blank=True)

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

The view has this (doctrina/views.py):

from django.shortcuts import render

# Create your views here.
def home_page(response):
    return render(response, "home/home_page.html")


# Create your views here.
def doctrina_index_page(response):
    return render(response, "doctrina/doctrina_index_page.html")

In promote tab, it has this url:

doctrina-elim

But entering that after the hostname gives 404, why?

enter image description here

Structure:

-doctrina
    |__pycache.py_
    |__migrations.py
    |__templates
    |___init__.py
    |_admin.py
    |_apps.py
    |_models.py
    |_test.py
    |_urls.py
    |_views.py

-elim
     |__pycache_.py
     |_settings.py
     |_static
     |_templates
     |___init__.py
     |_urls.py
     |_wsgi.py
-home
     |__pycache_.py
     |_migrations.py
     |_static
     |_templates \ home
        |_home_page.html
        |_welcome_page.html
     |___init__.py 
     |_models.py
     |_urls.py
     |_views.py

I can only access my Page models when setting them as sites, and visit the "/" home.

Upvotes: 0

Views: 2248

Answers (2)

Dan Swain
Dan Swain

Reputation: 3098

With Wagtail you will likely find that your views.py file does not have a lot in it. Rather, you create links to navigate to the page, and then Wagtail serves up the page - you don't have to render it from a view as you are doing. Here is some example code of how you might render a navigation bar:

{% load wagtailcore_tags %}

<ul>
    {% for item in request.site.root_page.get_children.live.in_menu %}
        <li>
            <a href="{% pageurl item %}">{{ item.title }}</a>
        </li>
    {% endfor %}
</ul>

This code only shows a single-level menu/page tree (not sub-menus), and it doesn't show applying a class name to indicate the active menu item - it only shows the basics of creating a menu. To conclude: whereas in Django you render and create associated context from within views.py, Wagtail takes care of rendering the page without the need of anything in views.py. However, if you need to add context to a Wagtail page, then define get_context(self, request) for the page: https://docs.wagtail.io/en/latest/topics/pages.html#customising-template-context.

Upvotes: 1

gasman
gasman

Reputation: 25227

As mentioned at How to set the URL for View using Wagtail? , the doctrina-elim page needs to be made a CHILD page of the homepage.

You can do that from the page listing (this page) by clicking on 'More' underneath the page, then 'Move'.

If you don't do that, Wagtail will not treat it as belonging to the same site.

Upvotes: 0

Related Questions