Синтаксис re_path в *приложении* urls.py

Я получаю эту ошибку уже почти неделю. Я гуглил, проверял документацию и искал видео на youtube. Я не могу найти ответ на этот, казалось бы, простой и очевидный вопрос: Каков синтаксис для re_path() во включенных урлах из моих приложений?

ошибка:

Reverse for 'jhp_url' with keyword arguments '{'slug': ''}' not found. 1 pattern(s)  
tried: ['(?P<twodigit>[a-z]{2,3})/courts/(?P<slug>[-a-zA-Z0-9_]+)/$']

Этот шаблон является правильным! Итак, очевидно, что проблема в том, что slug содержит пустую строку. Но почему? Я проверил это через reverse():

 def get_absolute_url(self):
    return reverse('jhp_url', kwargs={'slug': self.slug})

Q1:Почему он не видит kwarg из reverse() и использует self.slug?

Если я пытаюсь поместить self.slug в представление или url в качестве дополнительных аргументов, PyCharm жалуется.

Постановка пространства имен в reverse() и шаблоне не дает абсолютно никакой разницы! Я получаю ту же ошибку.

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) в отличие от наличия его в одном, но не в другом

Похоже, что первая ошибка, о которой я упоминал здесь, ближе к правильной. Мой контекст шаблона debug_toolbar говорит:

 'slug': '^(?P<slug>[-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<slug>[-a-zA-Z0-9_]+)/$"}, 
            name='tdr'),
re_path(r"(?P<slug>[-a-zA-Z0-9_]+)/$", courtdetail, name='jhp_url'),

Конечно, я все еще получаю эти ошибки, но я хочу сказать, что интерпретатор работает с этим. Но когда я пробую такие вещи, как

    re_path(r"^$", twodigit_testt1, {'slug': r'^(?P=slug)/$'}, name='tdr'),

Я просто получаю синтаксические ошибки.

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

Вернуться на верх