re_path syntax in application urls.py
I have been getting this error for almost a week. I have googled and checked the docs and looked for youtube videos. I cannot find an answer to this seemingly simple and obvious question: What is the syntax for re_path() in the included urls from my apps?
error:
Reverse for ‘jhp_url’ with keyword arguments ‘{‘slug’: ‘’}’ not found. 1 pattern(s) tried: [‘(?P[a-z]{2,3})/courts/(?P[-a-zA-Z0-9_]+)/$’]
That pattern is correct! So obviously, the problem is slug has an empty string. But why? I have it in reverse():
def get_absolute_url(self): return reverse(‘jhp_url’, kwargs={‘slug’: self.slug})
Q1:Why isn’t it seeing the kwarg from reverse() and using self.slug?
If I try to put self.slug in the view or the url as extra arguments, PyCharm complains.
Putting the namespace in reverse() and the template makes absolutely no difference! I get the same error.
BUT, if I take the namespace out of those two(1) places, I get a different error:
Reverse for ‘jhp_url’ not found. ‘jhp_url’ is not a valid view function or pattern name.
(1) as opposed to having it in one but not the other
So it seems like the first error I mentioned here is closer to being right. My debug_toolbar template context says:
‘slug’: ‘^(?P[-a-zA-Z0-9_]+)/$’
I’m pretty sure that’s wrong. It should be the actual slug and not the pattern. That’s why I have focused on the app urls.py. But, as I said at the top of this rant. I have not been able to find anything on the syntax of re_path() in the included app urls!
bench.urls.py:
urlpatterns = [ re_path(r"^$“, twodigit_testt1, {‘slug’: r”^(?P[-a-zA-Z0-9_]+)/$“}, name=‘tdr’), re_path(r”(?P[-a-zA-Z0-9_]+)/$", courtdetail, name=‘jhp_url’),
Of course I still get these errors, but my point here is that the interpreter runs with that. But when I try things like
re_path(r"^$", twodigit_testt1, {‘slug’: r’^(?P=slug)/$'}, name=‘tdr’),
I just get syntax errors.
Finally, please note that these errors are coming because the list template that twodigit_test1 is calling has urls to the individual detail pages in it. If I take the detail urls out of the template, it works. But if I go directly to the detail page, after importing my app views into the project urls, that works, too! It’s only the list template + detail urls combination that is the problem - and if you can’t list your details on your list page, what’s the point? I have tried both the url template tag and get_absolute_url in the template. Finally, I did ask an earlier version of this question on SO. I know some people don’t like that but it did not resolve this issue. I have reworked and refocused the question so it is not identical. Plus, I wasn’t using re_path() then.