testt1 is my view for taking a queryset and turning out a list of objects and their foreign key children.
mydetail is the detail view for the individual objects. No slug is used in the list, but it is in the list template to call the detail pages. But all these errors come from trying to call the list, not the detail. If I just type the slug into the address bar, all my detail pages come up just fine!
I have tried a lot of different things, and get a lot of different errors, but the bottom line is always the same: no list
named url fails
Error during template rendering In template /home/malikarumi/Projects/hattie/templates/base.html, error at line 44 Reverse for 'jhp_url' not found. 'jhp_url' is not a valid view function or pattern name.
‘jhp_url’ is the named url for the detail page.
But here is line 44:
<link href="{% static 'plugins/owl-carousel/owl.carousel.css' %}" rel="stylesheet">
the urlpattern:
path('<slug:slug>/', mydetail, name='jhp_url'),
the call to reverse():
def get_absolute_url(self):
return reverse(‘jhp_url', kwargs={'slug': self.slug})
- namespaced url fails
Error during template rendering In template /home/malikarumi/Projects/hattie/templates/base.html, error at line 44 Reverse for 'jhp_url' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['(?P[^/]+)/courts/(?P[-a-zA-Z0-9_]+)/$']
Now it has an empty string for the slug. The pattern is right. If it had a slug, it would work.
def get_absolute_url(self):
return reverse('bench:jhp_url', kwargs={'slug': self.slug})
list template:
<h3><a href="{% url 'bench:jhp_url' slug=C.slug %}">{{ C.name }}</a></h3>
urlpattern is the same.
no slug passed
testt1() missing 1 required positional argument: 'slug'
Which means that as I have currently defined it:
def testt1(request, slug, *args, **kwargs):
print(request.slug)
Is not being provided the slug variable / value. Now it might also be a question of position, so I changed only this:
def testt1(request, *args, **kwargs):
But since slug is still in the return value, now I get: Unresolved reference 'slug'
I take it out, and the autoreloader wakes up…
'WSGIRequest' object has no attribute 'slug'
Yes, well, that's what I suspected. That’s why I had the print in there. The question is: why does it not have slug?
https://docs.djangoproject.com/en/3.2/topics/http/urls/#including-other-urlconfs Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
Apparently, I don’t understand this process, because I always assumed this meant the slug part of my url was being passed along as part of the request object. But now I see that the word 'slug' does not appear in the docs on the response request page: https://docs.djangoproject.com/en/dev/ref/request-response/
- extra arguments fail
The docs also give the option of passing ‘extra arguments’: https://docs.djangoproject.com/en/3.2/topics/http/urls/#passing-extra-options-to-include https://docs.djangoproject.com/en/3.2/topics/http/urls/#passing-extra-options-to-view-functions
But this says nothing about using extra arguments in conjunction with named or even namespaced urls. Does that mean they don't, or can't go together? Then shouldn’t they say so explicitly, like the Zen of Python? Anyway, my efforts in this area all failed miserably:
File "/home/malikarumi/Projects/hattie/bench/urls.py", line 13
path('<slug:slug>', mydetail, name='jhp_url', {'slug': slug}),
^
SyntaxError: positional argument follows keyword argument
Are they saying a dictionary is a positional argument, and not a keyword?
File "/home/malikarumi/Projects/hattie/bench/urls.py", line 14, in <module>
path('', testt1, slug='slug', name='testt1'),
TypeError: _path() got an unexpected keyword argument 'slug'
File "/home/malikarumi/Projects/hattie/bench/urls.py", line 13, in <module>
path('<slug:slug>', mydetail, slug='slug', name='jhp_url'),
TypeError: _path() got an unexpected keyword argument 'slug'
File "/home/malikarumi/Projects/hattie/bench/urls.py", line 13, in <module>
path('<slug:slug>', mydetail, name='jhp_url', slug=slug),
NameError: name 'slug' is not defined
It's not? then wtf is 'slug:slug'???
Clearly, the slug is not being passed. If it isn't happening automatically, and it isn't part of the request object - despite slug:slug in the urlpattern, and I can't use these extra arguments, how am I supposed to get the slug passed?!
- explicit slug keyword argument in view fails:
File "/home/malikarumi/Projects/hattie/bench/views.py", line 22
def testt1(request, *args, '<slug:slug>', **kwargs):
^
SyntaxError: invalid syntax
Just in case you wondering, yes, I did try explicitly passing the slug as a keyword. All such efforts failed.
def testt1(request, slug=slug, **kwargs):
unresolved reference slug
def testt1(request, slug=self.slug, **kwargs):
unresolved reference self
UPDATE As requested, here is my latest version of views and urls, and at the bottom a link to the full stacktrace going back to yesterday.
VIEWS
from django.shortcuts import render
from bench.models import Jurisdiction
from django.shortcuts import get_object_or_404
from statedict import sdl
j = Jurisdiction.objects.all()
from urllib.parse import urlparse
def mydetail(request, slug, **kwargs):
court = j.get(slug=slug)
# twodigit = twodigit
return render(request, 'bench/jhp_.html',
{'court': court})
def testt1(request, **kwargs):
federalcourts = j.filter(sphere="Appeals").filter(purview="Federal")
circuits = federalcourts.order_by('siblingrank')
scotus = circuits.first().name
context = {'circuits': circuits, 'scotus': scotus, 'slug': slug}
return render(request, 'bench/listtest1.html', context)
URLS
from django.urls import path
from bench.views import mydetail, testt1
app_name = 'bench'
urlpatterns = [
path('/', mydetail, name='jhp_url'),
path('', testt1, name='testt1'),
]
STACKTRACE:
https://docs.google.com/document/d/1ktm-y_AEUrllw0kSctlLgUyNfxVx6eFnYlOZCs-jM2w/edit?usp=sharing
thank you