Urlpatterns missing a parameter but not sure how to install it

We have a Django app that’s working for some URLs but not all.

from django.urls import include, re_path
from . import views

app_name = 'devices'
urlpatterns = [
    re_path(r'^show/[A-Za-z0-9]+$', views.show_device, name='show'),
    re_path(r'^show/(?P<asset_tag>[\w@./#&+-]+)$', views.show_device, name='show'),
    re_path(r'^depot/(?P<asset_tag>[\w@./#&+-]+)$', views.depot_device, name='depot'),
    re_path(r'^ping/(?P<ip>([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})+)$', views.device_ping, name='ping'),

    re_path(r'^depotToStore/(?P<device_id>\d+|)$', views.depot_to_store, name='depotToStore'),
    re_path(r'^depotToStoreInformation/(?P<device_id>\d+|)?/(?P<store_code>\d+|)$', views.depot_to_store_information, name='depotToStoreInformation'),
    re_path(r'^create/store_id/(?P<store_id>\d+)/ipblock_id/(?P<ipblock_id>\d+)/$', views.add_device, name='add_a_device'),
    re_path(r'^devicetypes/(?P<pk>\d+)$', views.get_devicetype_info, name='devicetypes'),
    re_path(r'^edit/(?P<device_id>\d+|)$', views.edit_device, name='edit'),
    re_path(r'^delete/(?P<device_id>\d+|)$', views.delete_device, name='delete'),
]

The URL in one template is this one and works fine:

   <td> {% if device.asset_tag %}<a href="{% url 'devices:show' device.asset_tag %}">{{ device.asset_tag }}</a> {% endif %}</td>

But a similar one from another template fails:

<td><a href="{% url 'devices:show' device.asset_tag %}">{{ device.asset_tag }}</a></td>
# TypeError at /devices/show/router400960

show_device() missing 1 required positional argument: 'asset_tag'

Seems like there must be a setup for the asset_tag parameter that’s missing, but I don’t see how to add it. Help please/

You need to specify the “asset_tag” name in the first regex in the same way you have it defined for the second. See URL dispatcher | Django documentation | Django

(I’d be curious as to why you have two different regexes for the same view - I’m not sure this is going to do what you’re hoping for this to do - but that’s just a guess. My impression is that you could just remove that first re_path definition.)

The first one wasn’t working for the first url, which is why I added the other one.

I tweaked the top line and added another, giving this:

 re_path(r"^show/[A-Za-z0-9]+", views.show_device, name='show'),
    re_path(r'^show/?P<asset_tag>$', views.show_device, name='show'),
    re_path(r'^show/(?P<asset_tag>[\w@./#&+-]+)$', views.show_device, name='show'),

The error I get with the original template that’s been causing problems now is the same that I have in my original message, and my question remains: how to register the <asset_tag> parameter with the URL dispatcher?

Look at your third entry. You defined the name of the variable with the regex in that defintion. That’s what you need to do with your other url.

My latest try for a pattern:

re_path(r'^show/(?P<asset_tag>[A-Za-z0-9]$)', views.show_device, name='show'),
    re_path(r'^show/(?P<asset_tag>[\w@./#&+-]+)$', views.show_device, name='show'),

This produces the error:

NoReverseMatch at /devices/show/router400960
Reverse for 'show' with arguments '('',)' not found. 4 pattern(s) tried: ['devices/show/(?P<asset_tag>[\\w@./#&+-]+)\\.(?P<format>(json|html|xml|cgi))/?$', 'devices/show/(?P<asset_tag>[\\w@./#&+-]+)$', 'devices/show/(?P<asset_tag>[A-Za-z0-9]$)\\.(?P<format>(json|html|xml|cgi))/?$', 'devices/show/(?P<asset_tag>[A-Za-z0-9]$)']

I hope I have a simple syntax error now, but I can’t see what it is; thanks again for any help.

That’s not a problem with your url definition. This issue is either an issue with a view or a template. Please provide the complete error message with the traceback.

Thanks; here’s the whole thing:

Internal Server Error: /devices/show/router400960                                                                                                                                                          
Traceback (most recent call last):                                                                                                                                                                         
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 56, in inner                                                                                              
    response = get_response(request)                                                                                                                                                                       
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response                                                                                          
    response = wrapped_callback(request, *callback_args, **callback_kwargs)                                                                                                                                
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view                                                                                      
    return view_func(request, *args, **kwargs)                                                                                                                                                             
  File "/home/aotemp/new-personality-server-2.0/personality/devices/views.py", line 57, in show_device                                                                                                     
    return render(request, 'devices/show_device.html', context=context_list)                                                                                                                               
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/shortcuts.py", line 24, in render                                                                                                           
    content = loader.render_to_string(template_name, context, request, using=using)                                                                                                                        
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string                                                                                           
    return template.render(context, request)                                                                                                                                                               
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render                                                                                            
    return self.template.render(context)                                                                                                                                                                   
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 175, in render                                                                                                      
    return self._render(context)                                                                                                                                                                           
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 167, in _render                                                                                                     
    return self.nodelist.render(context)                                                                                                                                                                   
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render                                                                                                     
    return SafeString("".join([node.render_annotated(context) for node in self]))                                                                                                                          
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>                                                                                                 
    return SafeString("".join([node.render_annotated(context) for node in self]))                                                                                                                          
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated                                                                                            
    return self.render(context)                                                                                                                                                                            
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/loader_tags.py", line 157, in render                                                                                               
    return compiled_parent._render(context)                                                                                                                                                                
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 167, in _render                                                                                                     
    return self.nodelist.render(context)                                                                                                                                                                   
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render                                                                                                     
    return SafeString("".join([node.render_annotated(context) for node in self]))                                                                                                                          
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>                                                                                                 
    return SafeString("".join([node.render_annotated(context) for node in self]))                                                                                                                          
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated                                                                                            
    return self.render(context)                                                                                                                                                                            
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/loader_tags.py", line 63, in render                                                                                                
    result = block.nodelist.render(context)                                                                                                                                                                
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render                                                                                                     
    return SafeString("".join([node.render_annotated(context) for node in self]))                                                                                                                          
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>                                                                                                 
    return SafeString("".join([node.render_annotated(context) for node in self]))                                                                                                                          
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated                                                                                            
    return self.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/defaulttags.py", line 321, in render
    return nodelist.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render
File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated                                                                                            
    return self.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/defaulttags.py", line 321, in render
    return nodelist.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/loader_tags.py", line 208, in render
    return template.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 177, in render 
    return self._render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/loader_tags.py", line 54, in render
    result = self.nodelist.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/defaulttags.py", line 321, in render
    return nodelist.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/template/defaulttags.py", line 471, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/urls/base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "/home/aotemp/myvenv/lib/python3.8/site-packages/django/urls/resolvers.py", line 828, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'show' with arguments '('',)' not found. 4 pattern(s) tried: ['devices/show/(?P<asset_tag>[\\w@./#&+-]+)\\.(?P<format>(json|html|xml|cgi))/?$', 'devices
/show/(?P<asset_tag>[\\w@./#&+-]+)$', 'devices/show/(?P<asset_tag>[A-Za-z0-9]$)\\.(?P<format>(json|html|xml|cgi))/?$', 'devices/show/(?P<asset_tag>[A-Za-z0-9]$)']

From the traceback:

This shows that the error is occurring while attempting to render show_device.html, most likely in a {% url ... %} tag.

To fully diagnose this, then we’d need to see this view (show_device in personality/devices/views.py) and the template.

We can get started with you just showing the {% url tag where it’s trying to reference the url named 'show'.

Side note: You are showing two url entries for the same name. That isn’t going to do what you might think it should do. It will never generate a reference for the first named url. When trying to resolve url names, the last defined name is what applies. Now, since both urls are effectively the same except for the regex pattern, it’s probably not going to make a difference in practice. However, I’d still suggest renaming one of them.

Thanks I’ll take a look at that.

It fails on this line in the template:

                        <td><a href="{% url 'devices:show' device.asset_tag %}">{{ device.asset_tag }}</a></td>

Once again, I don’t see what’s wrong with it?

There is nothing wrong with that line itself. The error is somewhere else.

The error being reported is this:

django.urls.exceptions.NoReverseMatch: Reverse for 'show' with arguments '('',)' not found.

What this is specifically telling you is at the time that this line is being rendered:

the expression device.asset_tag does not have a value

At least one of the following is true:

  • device is not a reference to an object
  • The object that device is referencing does not have an attribute asset_tag
  • The attribute asset_tag does not have a value, is None, or is blank

To Figure out which of these is true is where it’s necessary to see the full template, view, and have knowledge of what data is being rendered at that specific time.

1 Like

But since device.asset_tag is also the text displayed and that looks fine; none of these can be true, right?

As a result of the error, the page isn’t being displayed - the rendering doesn’t finish.

If you’re seeing a page, that’s not the page generating the error.

I have a screenshot here that’s showing it is that line … and the asset tag must be the router32000… string …

I’m still missing something, but does this clear up the question?

So it’s trying to render the page for /devices/show/router320000-1920822, and is failing while trying to do that.

It is not saying there is anything wrong with the url /devices/show/router320000-1920822 itself. The problem is in that view trying to render its page.

So, the view devices.views.show_device is failing when router320000-1920822 has been passed as the parameter. That’s what you need to look at.

Thanks, but it fails on most others also. I noticed one or two work earlier but I didn’t have a screen recording on earlier so I didn’t get it captured.

Then there’s something wrong with either the view or the template attempting to be rendered on that page. (The view being executed for those urls, devices.views.show_device or the template stores/list_devices.html.)

Again:

OK … my first front-end work in quite a while and it’s been quite a challenge. Thanks again for your help.

Thanks again! I just fixed it … devices object came out as a list and just had to be unpacked to get to the underlying object.