No Reverse Match and namespaced urls

Note: I have already posted a related feature request here: No Reverse Match and namespaced urls
After repeatedly hitting NoReverseMatch, trying everything I know, and consulting the docs, I have turned here for help.

models.py

    def get_absolute_url(self):
        from django.urls import reverse
        return reverse('xedoc:detail', kwargs={'slug': self.slug})

project urls.py

    path('xedoc/', include('xedoc.urls', namespace='xedoc:detail'))

app urls.py

app_name = 'xedoc'
urlpatterns = [
    path('<slug:slug>', views.DetailView.as_view(), name='detail')
]

error

Reverse for 'detail' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['xedoc/(?P<slug>[-a-zA-Z0-9_]+)\\Z']

In the error, if ‘slug’ is the key, then I have to assume the missing value is the actual slug of my instance. It has one. They all do. Why can’t it be found?

Thank you.

We would need to see the template being rendered when this error occurs and the view that is attempting to render the template where this error occurs.

Yes, this is an issue where you’re trying to reverse a url that requires a parameter and no parameter is being supplied.

I will point out that this error has nothing to do with the type of issue that you were trying to describe in the linked ticket. The call to reverse is finding the entry. It’s the parameter that is the problem.

Note the + in this pattern says One or more of the previous and not zero or more (which would be *) so it doesn’t match the empty string ''.

One or more of the previous characters in the regex pattern, so the empty string does not match. I get that. But why is it getting the empty string? I will post this in a second, but the model has a slug field. Every instance of this model class has a slug field. Why is it being presented an empty string? Where is that coming from?

As I said to @carltongibson, I will post that momentarily. But I am subclassing DetailView. My understanding that parameters or attributes like SingleObjectMixin and slug_url_kwarg are supposed to act automatically. Is that wrong?

My experience has been that talking in the abstract about such things in the absence of seeing the code is rarely useful. I’m going to wait to see the code before making any substantial comment.

If this is where the reverse call is being made then it’s
because self.slug is empty.

@KenWhitesell @carltongibson Gentlemen, the problem is of my own making. I know you are shocked and surprised. I certainly was. The short version is this is part of a long, complex processing script (or pipeline, if you prefer). But I neglected to make sure that the slugs actually got created, having become used to prepopulated fields in admin.py doing it for me. And therein lies the whole of my problem. It is now fixed. A few more bumps last night, but nothing I couldn’t fix, and the whole thing runs beautifully. Thank you for your time, your thoughts, and your patience.

1 Like

I see you have Project Urls. the namespace refers to an app called xedoc:detail , but you app called xedoc only.
so your project url search in different urls i guess.
as i think i would be better to use urls like below.
Project Urls.py:

path('xedoc/', include('xedoc.urls', namespace='xedoc'))

xedoc app Urls.py:

path('<slug:slug>', views.yourview, name='detail')

i hope it was useful this little note.

Yes, I’ll take all the clarifying help I can get. Shokran.

1 Like