1

我有几个位置的 django 数据模型,我需要从 wagtail 中生成页面。像博客一样,我需要一个列表页面和详细信息页面。

我遵循了这个文档:https ://wagtail-app-pages.readthedocs.io/en/latest/index.html

模型.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"

网址.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"),
]

视图.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_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 %}

我可以选择在 wagtail 中选择“位置列表”页面。我创建了页面,唯一可用的字段是内部名称字段,这是正确的。

但是,当我转到 URL 时,内容块是空白的。当我尝试去一个位置时,即'location/1',我得到一个 404。

我认为正在发生的事情是我的模型被忽略了,wagtail 页面应用程序被忽略了,并且 Locations List 被视为空白 wagtail 页面模型。我不明白我做错了什么。控制台中没有错误。

4

2 回答 2

1

很抱歉我发现这有点晚了,但我会尝试以作者的身份参与进来。

我只是试图在本地重现该问题。我不得不对代码进行一些调整,我不确定您使用的是什么版本的 django 和 wagtail,但我设法根据您的代码进行了基本设置。

它很快就被破解了,但是请随意看看这个,它是一个包含内容的 sqlite 数据库的 repo,您​​可以使用 admin/admin 登录。

老实说,我对 URL 配置的工作方式不清楚的地方有点困惑,因为它非常接近 django url 配置的原则。您的示例似乎确实使用了一种奇怪的语法组合,混合了正则表达式和非正则表达式的语法。我在我的示例中进行了一些更改,同时显示了正则表达式和非正则表达式模式(django 3.0)。

请让我知道这是否可以解决问题,我很高兴收到有关如何改进文档的任何建议。

于 2020-07-30T11:03:02.963 回答
0

文档中的示例未正确演示 URL 与此库一起使用的方式。

Wagtail 设置列表页面的 URL。我使用 LocationPage 模板创建了一个页面,将其命名为“Locations”,并且可以在“/locations”中访问它。我的 url.conf 是错误的。我将列表页面设置为 r'^locations/?$'。

  1. wagtail 应用程序页面由于某种原因不喜欢正则表达式。我很想知道为什么,但这是不行的。

  2. 我选择的 LocationListView slug 必须添加到 wagtail 页面 URL 的路径中。删除正则表达式,如果我在我的 URL conf 中使用 'locations/',那么我的页面可以在 'locations/locations/' 访问。

我认为文档假定您的 wagtail 页面 URL 将是 root(这没有意义),因此可以在 root/articles 访问博客列表页面。但是,据我所知,这会导致您的根 URL 被声明但未被使用,因此您将没有主页。非常混乱。

于 2020-07-05T12:11:58.687 回答