【问题标题】:How to set the URL for View using Wagtail?如何使用 Wagtail 设置 View 的 URL?
【发布时间】:2020-05-27 01:47:52
【问题描述】:

Wagtail 是否只使用“Promote Tab”中的 slug,或者我应该将 url 设置为 urls.py?

我正在使用 Wagtail,我有 2 页:

a)Ministerios Internacionales Elim
b)Doctrina Elim

我想将 a 设为首页,b 设为:

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

现在doctrina/urls.py 是空的:(

我在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")
    ]

视图有这个(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")

promote tab,它有这个网址:

doctrina-elim

但是在主机名之后输入404,为什么?

结构:

-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

我只能在将它们设置为站点时访问我的页面模型,并访问“/”主页。

【问题讨论】:

    标签: django wagtail


    【解决方案1】:

    使用 Wagtail,您可能会发现您的 views.py 文件中没有很多内容。相反,您创建链接以导航到该页面,然后 Wagtail 提供该页面 - 您不必像正在做的那样从视图中呈现它。下面是一些如何呈现导航栏的示例代码:

    {% 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>
    

    此代码仅显示单级菜单/页面树(不是子菜单),并且未显示应用类名来指示活动菜单项 - 它仅显示创建菜单的基础知识。总结一下:在 Django 中,您从 views.py 中渲染和创建关联的上下文,Wagtail 负责渲染页面,而不需要 views.py 中的任何内容。但是,如果您需要向 Wagtail 页面添加上下文,则为页面定义 get_context(self, request)https://docs.wagtail.io/en/latest/topics/pages.html#customising-template-context

    【讨论】:

    • 我明白了,你能告诉我你从哪里得到pageurl 变量吗?我刚刚将您的代码复制到我的 navbar.html 中,得到:TemplateSyntaxError at / Invalid block tag on line 16: 'pageurl', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
    【解决方案2】:

    How to set the URL for View using Wagtail? 所述,doctrina-elim 页面需要设为首页的 CHILD 页面。

    您可以从页面列表 (this page) 中执行此操作,方法是点击页面下方的“更多”,然后点击“移动”。

    如果您不这样做,Wagtail 不会将其视为属于同一站点。

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 2021-01-07
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多