djangodjango-modelswagtailwagtail-pageurl

How do I use Wagtail App Pages to create dynamic pages from django model?


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.


Solution

  • 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.