im new in django, recently i want to use a button in html and go to admin page or more exactly to user management page. how can i do it? by simply using
add user and
path(‘admin/’, admin.site.urls, name=‘admin’),
it just cause a error like: Reverse for ‘admin’ not found. ‘admin’ is not a valid view function or pattern name.
from the trail i know how to find where the admin html pages locate in my pc,but i still messed up by how to redirect to admin pages
i think my english is poor ,if you think i need show your more details pls tell me.
thx!
The names of URLs in Django Admin are of the form:
"admin:<app_label>_<model_name>_change"
(admin
is the namespace of the URLs)
So to create a link in a template to go to the “change” page for the User
model, which is in the auth
app:
<a href="{% url "admin:auth_user_change" user.pk %}">Edit user</a>
Assuming that user
is a variable that’s a User
object.
And if you wanted to link to the form for adding a new user, you’d use the “add” page instead of the “change” page, and you wouldn’t need user.pk
:
<a href="{% url "admin:auth_user_add" %}">Add user</a>
You can also keep it simple and make a plain link without using {% url %}
/reverse
.
thx a lot ! it just perfectly works
So long as you never change Django Admin’s URL
Or if you do, just grep for the url and fix any links. It’s not like that’s hard. If you change the name of the admin in the path mapping, you’d have to grep for that and change it in all the places too. That seems roughly equally likely.
You could never use the url tag or reverse, for the same reason. I was trying to help someone by giving them the recommended, best practice answer.