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.