re_path syntax in *application* urls.py

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.

I’m sorry, but I can’t figure out what you’re working with from all these various snippets.

Let’s start with the basics. What does your urls.py file currently look like?
And more importantly what does the URL you’re trying to produce / reference look like?
It’s going to be a lot easier talking about this with referencing the specific code rather than trying to address this in the abstract.

(Note, when pasting code, please enclose it between lines of three backtick - ` characters. This means that you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to maintain the proper formatting of your code.)

project.urls.py:

from django.urls import include, re_path, path
from django.views.generic import TemplateView
from django.contrib import admin
from django.conf import settings
import debug_toolbar
import bench.views
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('library/', TemplateView.as_view(template_name='library.html')),
    # path('library/', views.libtest),
    # path('library/', views.Libcbv.as_view(template_name='library.html', slug='library')),

    path('admin/doc/', include('django.contrib.admindocs.urls')),
    path('admin/', admin.site.urls),
    # TODO: TemplateDoesNotExist at /admin/doc/ expecting name to be: admin_doc/index.html
    path('<twodigit>/', include([
        path('case/', include('caseAT.urls'), ),
        path('courts/', include('bench.urls'))],),),
]

application.urls.py

from django.urls import path, include
from bench.views import  mydetail, testt1
# from statedict import sdl
#  CourtDetail CourtList, appellate_list, test_t,

app_name = 'bench'

urlpatterns = [

    path('<slug:slug>/', mydetail, name='jhp_url'),
    path('', testt1, name='testt1'),
    # path('', test_t, name='test_t'),
    # path('', appellate_list),
    # path('us-supreme-court', mydetail, name='jhp_url'),
]

target / test url:
ttp://127.0.0.1:8090/us/courts/
took the h off so software would display
template:

 <li>California</li>
<li><a href="{% url 'jhp_url' state.slug %}">Colorado url tag</a></li>#} <--error
<li><a href="{{ state.get_absolute_url|safe }}">Colorado gau</a></li> <--html entities
<li><a href="http://127.0.0.1:8090/co/courts/colorado-supreme-court/">Colorado full domain</a></li>
     <--works
 <li><a href="co/courts/colorado-supreme-court/">Colo relative link</a></li> <--wrong, but works
 <li>Connecticut</li>

error:

Request URL:	http://127.0.0.1:8090/us/courts/
Django Version:	3.1.7
Exception Type:	NoReverseMatch
Exception Value:	
Reverse for 'jhp_url' not found. 'jhp_url' is not a valid view function or pattern name.

NOTE: The NoReverseMatch is not for us/courts/, but for the detail links inside it. Template line:

    <a href="{{ c.get_absolute_url|safe }}">{{ c.name }}</a>

It does not work with {% url … %} either. detail template / named url manually entered works:

http://127.0.0.1:8090/co/courts/colorado-supreme-court/
http://127.0.0.1:8090/us/courts/us-supreme-court/
http://127.0.0.1:8090/us/courts/10th-cir/
http://127.0.0.1:8090/us/courts/9th-cir/
http://127.0.0.1:8090/us/courts/district-minnesota/

All work, but I didn’t take screenshots, and just in the last 12 hours PyCharm and runserver have decided not to work together, so… when it rains, it pours… :disappointed:

Finally, also in the last 12 hours, I have come across a new suspect: the refusal of autoescape to work. I don’t know if there is any connection, but it was when rebooting after adding {% autoescape off %} to my templates that runserver stopped working, even though the venv actually is there:

(hattie) malikarumi@Tetuoan2:~/Projects/hattie$ python3 manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
(hattie) malikarumi@Tetuoan2:~/Projects/hattie$ source bin/activate
bash: bin/activate: No such file or directory
(hattie) malikarumi@Tetuoan2:~/Projects/hattie$ python
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> 

And after it pours, the lightning and thunder start … :cloud_with_lightning_and_rain:

Addressing only the url question at the moment, since you have an app_name specified in your urls.py file, try referring to that url name as bench:jhp_url

Single lines (or sections of lines) can be flagged as “do not interpret” by using a single backtick:
http://127.0.0.1:8090/us/courts