1

I have a django model of data for several locations that I need to generate pages from in wagtail. Like a blog, I need a listing page and detail pages.

I followed this documentation: 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 %}

I have the option to select a 'Locations List' page in wagtail. I create the page, the only field available is the internal name field, which is correct.

When I go to the URL though, The content block is blank. When I try to go to a location, i.e. 'location/1', I get a 404.

I think what is happening is that my model is being ignored, wagtail page apps is being ignored, and Locations List is being treated as a blank wagtail page model. I don't understand what I am doing wrong. There are no errors in the console.

JSum
  • 597
  • 9
  • 20

2 Answers2

1

I'm sorry I found this a bit late, but I'll try to weigh in as the author.

I just tried to reproduce the issue locally. I had to make a few tweaks to the code, and I'm not sure what versions of django and wagtail you're using, but I managed to get a basic setup working based on your code.

It's hacked together very quickly, but feel free to take a look at this, it's a repo including an sqlite database with content, you can log in with admin/admin.

To be honest, I'm a little confused about what was unclear about the way the URL config works, as it follows the principles of django url configs pretty closely. Your example does seem to use a strange mix of syntaxes, mixing regex and non-regex based syntax. I changed that a bit in my example, showing both a regex and non-regex pattern (django 3.0).

Please let me know if this clears things up, and I'm happy for any suggestions as to how I can improve the documentation.

mephisto
  • 661
  • 1
  • 6
  • 14
0

The examples in the docs do not properly demonstrate the way URLs work with this library.

Wagtail sets the URL for the listing page. I created a page using the LocationPage template, named it 'Locations', and it is accessible at '/locations'. My url.conf was wrong. I had the listing page set to r'^locations/?$'.

  1. wagtail app pages does not like regex for some reason. I'm to green to understand why, but it is a no no.

  2. what I choose as my LocationListView slug has to be added to the path of the wagtail page URL. removing the regex, if I use 'locations/' in my URL conf, then my page is accessible at 'locations/locations/'.

I think the documentation assumes that your wagtail page URL will be root (which wouldn't make sense) so the blog listing page would be accessible at root/articles. but, as far as I know, that would cause your root URL to be claimed but unused, so you wouldn't have a home page. Very confusing.

JSum
  • 597
  • 9
  • 20
  • 1
    basically... wagtail_app_pages does handle regexes, in exactly the same way django does. the difference is that the pattern only applies to urls "under" the url of the parent page. This is actually the entire point of making a page into a self-contained app. You are free to either have an empty "root" pattern to overwrite the normal page serving, or to leave the page as is, and have "sub-urls" for the actual views. This is entirely up to how someone wants to use it – mephisto Jul 30 '20 at 11:08