如何使用 Wagtail App Pages 从 Django 模型创建动态页面?
How do I use Wagtail App Pages to create dynamic pages from django model?
我有几个位置的 django 数据模型,我需要从 wagtail 中生成页面。像博客一样,我需要一个列表页和详情页。
我遵循了这个文档:https://wagtail-app-pages.readthedocs.io/en/latest/index.html
models.py
from django.db import models
from wagtail.core.models import Page
from wagtail_app_pages.models import AppPageMixin
from . import misc_options
class Feature(models.Model):
feature = models.CharField(max_length=50)
description = models.CharField(max_length=300)
class Meta:
ordering = ['feature']
verbose_name = "Feature"
verbose_name_plural = "Features"
def __str__(self):
return self.feature
class Location(models.Model):
template = 'locations/locations_list.html'
name = models.CharField(max_length=30, null=True, blank=False)
address = models.CharField(max_length=60, null=True, blank=False)
city = models.CharField(max_length=25)
state = models.CharField(
max_length=2, choices=misc_options.STATES, null=True, blank=False)
zipcode = models.CharField(max_length=5, null=True, blank=False)
lat = models.CharField(max_length=50, null=True, blank=False)
lon = models.CharField(max_length=50, null=True, blank=False)
phone = models.CharField(max_length=10, null=True, blank=False)
email = models.CharField(max_length=40, null=True, blank=False)
places_id = models.CharField(max_length=100, null=True, blank=True)
facebook_url = models.CharField(max_length=100, null=True, blank=True)
google_url = models.CharField(max_length=100, null=True, blank=True)
entity = models.CharField(max_length=10, null=True, blank=True)
truck_url = models.CharField(max_length=100, null=True, blank=True)
trailer_url = models.CharField(max_length=100, null=True, blank=True)
supplies_url = models.CharField(max_length=100, null=True, blank=True)
features = models.ManyToManyField(Feature)
class Meta: # noqa
ordering = ['name']
verbose_name = "Location"
verbose_name_plural = "Locations"
def __str__(self):
return self.name
class LocationPage(AppPageMixin, Page):
template = 'locations/locations_list.html'
url_config = 'location.urls'
class Meta: # noqa
verbose_name = "Locations List"
verbose_name_plural = "Locations Lists"
urls.py
from django.urls import path
from .views import LocationDetailView, LocationListView
urlpatterns = [
path(r"^locations/?$", LocationListView.as_view(), name="location_list"),
path(r"^location/<int:pk>/?$",
LocationDetailView.as_view(), name="location"),
]
views.py
from django.shortcuts import render
from django.views.generic import DetailView, ListView
from .models import Location
from .google import getGoogleReviews
from .wss import getWSSUnits
class LocationDetailView(DetailView):
model = Location
context_object_name = "location"
queryset = Location.objects.all()
template_name = "locations/location_details.html"
def get(self, request, **kwargs):
self.object = self.get_object()
places_id = self.object.places_id
entity = self.object.entity
reviews = getGoogleReviews(places_id)
units = getWSSUnits(entity)
context = self.get_context_data(
object=self.object, reviews=reviews, units=units)
return self.render_to_response(context)
class LocationListView(ListView):
model = Location
locations/locations_list.html
{% extends 'base.html' %}
{% load app_pages_tags %}
{% block content %}
{% for item in location_list %}
{{item.name}}
{% endfor %}
{{ location_list }}
This is the locations list
{% endblock %}
我可以选择 select 'Locations List' wagtail 页面。我创建页面,唯一可用的字段是内部名称字段,这是正确的。
当我转到 URL 时,内容块是空白的。当我尝试前往某个位置时,即 'location/1',我收到 404。
我认为正在发生的事情是我的模型被忽略了,wagtail 页面应用程序被忽略了,而 Locations List 被视为一个空白的 wagtail 页面模型。我不明白我做错了什么。控制台没有错误。
文档中的示例没有正确演示 URL 使用此库的方式。
Wagtail 为列表页面设置 URL。我使用 LocationPage 模板创建了一个页面,将其命名为 'Locations',并且可以在“/locations”访问它。我的 url.conf 错了。我将列表页面设置为 r'^locations/?$'.
wagtail 应用页面出于某种原因不喜欢正则表达式。我很想知道为什么,但这是不行的。
我选择的 LocationListView slug 必须添加到 wagtail 页面的路径 URL。删除正则表达式,如果我在我的 URL conf 中使用 'locations/',那么我的页面可以在 'locations/locations/'.
访问
我认为该文档假定您的 wagtail 页面 URL 将是根目录(这没有意义),因此可以在 root/articles 访问博客列表页面。但是,据我所知,这会导致您的根 URL 被声明但未被使用,因此您将没有主页。很混乱。
很抱歉我发现这个有点晚了,但我会尝试作为作者权衡一下。
我刚刚尝试在本地重现该问题。我不得不对代码进行一些调整,我不确定你使用的是什么版本的 django 和 wagtail,但我设法根据你的代码进行了基本设置。
它很快就被破解了,但请随意看一下this,它是一个包含带有内容的sqlite数据库的repo,您可以使用admin/admin登录。
老实说,我对 URL 配置工作方式的不清楚之处有些困惑,因为它非常接近 django url 配置的原则。您的示例似乎确实使用了一种奇怪的语法组合,混合了基于正则表达式和非正则表达式的语法。我在我的示例中对此做了一些更改,同时显示了正则表达式和非正则表达式模式 (django 3.0)。
如果这样可以解决问题,请告诉我,我很乐意就如何改进文档提出任何建议。
我有几个位置的 django 数据模型,我需要从 wagtail 中生成页面。像博客一样,我需要一个列表页和详情页。
我遵循了这个文档:https://wagtail-app-pages.readthedocs.io/en/latest/index.html
models.py
from django.db import models
from wagtail.core.models import Page
from wagtail_app_pages.models import AppPageMixin
from . import misc_options
class Feature(models.Model):
feature = models.CharField(max_length=50)
description = models.CharField(max_length=300)
class Meta:
ordering = ['feature']
verbose_name = "Feature"
verbose_name_plural = "Features"
def __str__(self):
return self.feature
class Location(models.Model):
template = 'locations/locations_list.html'
name = models.CharField(max_length=30, null=True, blank=False)
address = models.CharField(max_length=60, null=True, blank=False)
city = models.CharField(max_length=25)
state = models.CharField(
max_length=2, choices=misc_options.STATES, null=True, blank=False)
zipcode = models.CharField(max_length=5, null=True, blank=False)
lat = models.CharField(max_length=50, null=True, blank=False)
lon = models.CharField(max_length=50, null=True, blank=False)
phone = models.CharField(max_length=10, null=True, blank=False)
email = models.CharField(max_length=40, null=True, blank=False)
places_id = models.CharField(max_length=100, null=True, blank=True)
facebook_url = models.CharField(max_length=100, null=True, blank=True)
google_url = models.CharField(max_length=100, null=True, blank=True)
entity = models.CharField(max_length=10, null=True, blank=True)
truck_url = models.CharField(max_length=100, null=True, blank=True)
trailer_url = models.CharField(max_length=100, null=True, blank=True)
supplies_url = models.CharField(max_length=100, null=True, blank=True)
features = models.ManyToManyField(Feature)
class Meta: # noqa
ordering = ['name']
verbose_name = "Location"
verbose_name_plural = "Locations"
def __str__(self):
return self.name
class LocationPage(AppPageMixin, Page):
template = 'locations/locations_list.html'
url_config = 'location.urls'
class Meta: # noqa
verbose_name = "Locations List"
verbose_name_plural = "Locations Lists"
urls.py
from django.urls import path
from .views import LocationDetailView, LocationListView
urlpatterns = [
path(r"^locations/?$", LocationListView.as_view(), name="location_list"),
path(r"^location/<int:pk>/?$",
LocationDetailView.as_view(), name="location"),
]
views.py
from django.shortcuts import render
from django.views.generic import DetailView, ListView
from .models import Location
from .google import getGoogleReviews
from .wss import getWSSUnits
class LocationDetailView(DetailView):
model = Location
context_object_name = "location"
queryset = Location.objects.all()
template_name = "locations/location_details.html"
def get(self, request, **kwargs):
self.object = self.get_object()
places_id = self.object.places_id
entity = self.object.entity
reviews = getGoogleReviews(places_id)
units = getWSSUnits(entity)
context = self.get_context_data(
object=self.object, reviews=reviews, units=units)
return self.render_to_response(context)
class LocationListView(ListView):
model = Location
locations/locations_list.html
{% extends 'base.html' %}
{% load app_pages_tags %}
{% block content %}
{% for item in location_list %}
{{item.name}}
{% endfor %}
{{ location_list }}
This is the locations list
{% endblock %}
我可以选择 select 'Locations List' wagtail 页面。我创建页面,唯一可用的字段是内部名称字段,这是正确的。
当我转到 URL 时,内容块是空白的。当我尝试前往某个位置时,即 'location/1',我收到 404。
我认为正在发生的事情是我的模型被忽略了,wagtail 页面应用程序被忽略了,而 Locations List 被视为一个空白的 wagtail 页面模型。我不明白我做错了什么。控制台没有错误。
文档中的示例没有正确演示 URL 使用此库的方式。
Wagtail 为列表页面设置 URL。我使用 LocationPage 模板创建了一个页面,将其命名为 'Locations',并且可以在“/locations”访问它。我的 url.conf 错了。我将列表页面设置为 r'^locations/?$'.
wagtail 应用页面出于某种原因不喜欢正则表达式。我很想知道为什么,但这是不行的。
我选择的 LocationListView slug 必须添加到 wagtail 页面的路径 URL。删除正则表达式,如果我在我的 URL conf 中使用 'locations/',那么我的页面可以在 'locations/locations/'.
访问
我认为该文档假定您的 wagtail 页面 URL 将是根目录(这没有意义),因此可以在 root/articles 访问博客列表页面。但是,据我所知,这会导致您的根 URL 被声明但未被使用,因此您将没有主页。很混乱。
很抱歉我发现这个有点晚了,但我会尝试作为作者权衡一下。
我刚刚尝试在本地重现该问题。我不得不对代码进行一些调整,我不确定你使用的是什么版本的 django 和 wagtail,但我设法根据你的代码进行了基本设置。
它很快就被破解了,但请随意看一下this,它是一个包含带有内容的sqlite数据库的repo,您可以使用admin/admin登录。
老实说,我对 URL 配置工作方式的不清楚之处有些困惑,因为它非常接近 django url 配置的原则。您的示例似乎确实使用了一种奇怪的语法组合,混合了基于正则表达式和非正则表达式的语法。我在我的示例中对此做了一些更改,同时显示了正则表达式和非正则表达式模式 (django 3.0)。
如果这样可以解决问题,请告诉我,我很乐意就如何改进文档提出任何建议。