简体   繁体   English

你如何为 wagtail/django 页面提供一个自定义 url 来提供服务?

[英]How do you give a wagtail/django Page a custom url to serve at?

In wagtail/django how do you make a basic wagtail Page model , create the html template, and then tell that model to serve as a view for a specific url?wagtail/django ,你如何制作一个基本的wagtail Page model ,创建 html 模板,然后告诉该模型作为特定 url 的视图?

from django.db import models
from wagtail.wagtailcore.models import Page

class MyPage(Page):
  title = models.CharField(blank=True, null=True, max_length=255)
  #...

I want the url to register like我希望网址注册为

url(r'^monkey/(?P<slug>[A-Za-z0-9]+)$', ...)

But I don't have a common urls.py folder its stored outside of the project.但是我没有一个常见的urls.py文件夹,它存储在项目之外。 I tried using the RoutablePageMixin , but I believe that serves for subpages.我尝试使用RoutablePageMixin ,但我相信它适用于子页面。 I also know where to store the html template within the structure so that is not a problem.我也知道在结构中存储 html 模板的位置,所以这不是问题。

You might want something like:你可能想要这样的东西:

from django.http import Http404
from django.views import View
from wagtail.core.views import serve

class WagtailPageServeView(View):
    """
    A default way to serve up wagtail pages in urls
    """

    wagtail_slug = None

    def get(self, request, *args, **kwargs):
        if self.wagtail_slug:
            return serve(request, self.wagtail_slug)
        else:
            raise Http404(
                f"WagtailPageServeView missing 'wagtail_slug'. Cannot serve "
                f"request.path: {request.path}"
            )

Then you can do something like this in urls.py :然后你可以在urls.py做这样的事情:

urlpatterns += [
    re_path(r'^$', WagtailPageServeView.as_view(wagtail_slug='/'), name='home'),
]

In Wagtail, you are creating a page type and then creating instances of the page type in the CMS.在 Wagtail 中,您正在创建页面类型,然后在 CMS 中创建页面类型的实例。

Even if you are only going to use this page once, you still need to make a type.即使您只打算使用此页面一次,您仍然需要制作一个类型。 Following this logic, page urls are defined inside the CMS through the page's parent and slug .按照此逻辑,页面 url 通过页面的 parent 和slug在 CMS 内定义。 Most likely, you could achieve what you want through these means.最有可能的是,您可以通过这些方式实现您想要的。

If you want to override your page template and create a custom url, you can override the get_url_parts method on your model.如果要覆盖页面模板并创建自定义 url,可以覆盖模型上的get_url_parts 方法

Also, if you want to skip Wagtail for this page specifically, remember that Wagtail is just built over Django.另外,如果您想专门跳过此页面的 Wagtail,请记住 Wagtail 只是基于 Django 构建的。 You can always create a new module and define the url in that model.您始终可以创建一个新模块并在该模型中定义 url。 If you check your site urls.py , you will see the wagtail url patterns.如果您检查您的站点urls.py ,您将看到 wagtail url 模式。 You can change it to look something like this:您可以将其更改为如下所示:

urlpatterns = [ # other urls... url(r'wagtail/', include(wagtail_urls)), url(r'^monkey/', ('monkey_app.urls', 'monkey_app')), ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM