Reverse admin urls

I’m currently trying to add a link to an object in the admin interface in a view.
The Documentation on reversing admin URLs says the following should work

<a href="{% url opts|admin_urlname:'add' %}">Add user</a>

in my case this would be

<a href="{% url org|admin_urlname:'change' %}" org.id>Change Rescue Orgnaization</a>

However I get the following error

'RescueOrganization' object has no attribute 'app_label'

I know that I need an app label

The opts variable can be any object which has an app_label and model_name attributes and is usually supplied by the admin views for the current model.

However I don’t understand why RescueOrganizations has no app label? It’s in my normal models.py and just as a sanity check I had a look in the DB and the table is called
fellchensammlung_rescueorganization.

I also tried setting the app label, without any effect.

Any idea what could cause this?

If you want to check out the source here are some links (but I’d also be happy to provide the relevant snippets)

  • views.py
  • other links in a response, as I’m a new user I can only post two links :grinning_face:

Other things I tried are reversing it in the views.py like this

change_url = reverse("admin:fellchensammlung_rescue_org_change", args=(org.id,), current_app="fellchensammlung")

but that failed with

Reverse for 'fellchensammlung_rescue_org_change' not found. 'fellchensammlung_rescue_org_change' is not a valid view function or pattern name.

Thanks for taking your time to read it!

Welcome @moanos !

While I can’t verify everything here, I do see that:

is constructed incorrectly.

The org.id needs to be part of the url tag to pass as a parameter to the ‘change’ url.

Notice the difference in the location of the parameter between what you have above and the sample in the docs:
<a href="{% url opts|admin_urlname:'delete' user.pk %}">Delete this user</a>

Also, the models themselves do not have app_label and model_name attributes. They’re exposed in the _meta attribute of the model. This means you should use something like {% url org._meta|admin_urlname:'change' ...

1 Like

Also, you do not have a model named rescue_org. For the purpose of the reverse function, I would expect you to need to use 'fellchensammlung_rescueorganization_change'

1 Like

Oh yes. Sorry I seem to have pasted this wrong. this is what I actually have in the code

<a href="{% url org|admin_urlname:'change' org.pk %}">Admin interface</a>

would that be correct on it’s own?

You are absolutely right, however I also tried this and it results in the same error

NoReverseMatch at /tierschutzorganisationen/2900/

Reverse for 'fellchensammlung_rescueorganization_change' not found. 'fellchensammlung_rescueorganization_change' is not a valid view function or pattern name.

Sorry for the mess, I tried so many things and pasted the wrong attempts in my initial post.

Thank you again, I got your response now after sleeping over it!

In the template I now have

# detail-rescue-org.html
<a href="{% url org_meta|admin_urlname:'change' org.pk %}"><i class="fa-solid fa-tools fa-fw"></i> Admin interface</a>

This is a slight difference to what you proposed as I can’t access ._meta in a template (Variables and attributes may not begin with underscores: 'org._meta')

I therefore added the following to my context:

#views,py
org = RescueOrganization.objects.get(pk=rescue_organization_id)
    org_meta = org._meta
    return render(request, template,
                  context={"org": org, "org_meta": org_meta})

Thanks again for your help @KenWhitesell !

If you are still interested in resolving this one, please post your reverse function call code.