redirect to specific user-entry from template

Hi there, title says it:

is it possible to perform a redirection by buttonclick in template to reach directly a specific user-entry in admin panel? And extended: with a context-based url?

what i want to do is reach (for example):
http://127.0.0.1:8000/admin/auth/user/1/change/

by doing something in template like:

<button type="submit" class="btn btn-transparent p-0"
  formaction="{% url 'admin:auth_user_{{ user.pk}}_change' %}">
  {{ user.username }}
</button>

where {{user.pk}}would be “1”, of course…

i tried it with the informations from
Django Dokumentation ReverseAdmin URL

But don’t get it done right I’m afraid…
according to this dokumentation, it must be something like:

{% load admin_urls %}
formaction="{% url 'admin:auth_user'|admin_urlname:'change' user.pk %}">

But I only get an 'SafeString' object has no attribute 'app_label'Error…

Actually, according to the docs it would be:
formaction="{% url 'admin:auth_user_change' user.pk %}">

The docs identify the id as being a parameter to the reverse function, not a part of the url name.

1 Like

Hmm…first of all: You are right…as always :wink:
Second: So if this is the needed Syntax, then i can understand it, but don’t feel that i have read something like that in the docs…there was for template issues the passage with the |admin_urlname stated…or maybe i had a problem with the language barrier.

Third: With your solution, i get the desired admin-page, but it is not the same like i would access it right though admin-panel: (both screenshots are taken from same URL, first accessed via admin-panel, second accessed via template)…so looks like there is kind of lacking another “instance” info while accessing via template?

FIRST:


SECOND:

But THX again for your work :slight_smile:

There are two different conditions / situations. First, quoting from the docs (slightly edited):

Each ModelAdmin instance provides an additional set of named URLs:

Page URL name Parameters
Change {{ app_label }}_{{ model_name }}_change object_id

This shows directly the syntax I provided above, where “app_label” is “auth” and “model_name” is “user”. So ‘admin:auth_user_change’ is the name of the url, being provided as a string literal, and the user.pk is the parameter.

The other situation you reference:

Comes from this:

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

However, there’s a critical different here.

The opts being used here is a variable, not a constant. It’s defined as:

The opts variable can be any object which has an app_label and model_name attributes…

Also, from the docs for the url tag:

The first argument is a URL pattern name. It can be a quoted literal or any other context variable.

So, ‘admin:auth_user_change’ is a quoted literal, while opts is a context variable.

I would need to see the urls being issed by the browser in both cases - your screen shots don’t show that.

Superficially, it looks like you might be trying to get to that page via POST instead of GET. You don’t want to do that. If you’re using that url to get to the admin page, your initial reference to that page should be a GET.

1 Like

And again, You are right. I understand well your statement to
Each ModelAdmin instance provides an additional set of named URLs:
but I simply didn`t select them as possible soluation according to the following passage with the template example…

And in the end: of COURSE it was a POST not GET Problem…after altering that, now it works like intended. I thank you once more…must be the 100st time :wink: