Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do create url to django Admin within my user page (HTML)

I'm new to coding, I'm trying to create URL link in NavBar to redirect my Django Admin exmple:

<a class="nav-link" href="{% url 'admin'%}"> Admin site </a>

However, Django do find; No-Reverse-Match,

It works for other URL links

Things I had to try:

  1. Is adding a name in project URL
  2. including URL path in my App URL

My project URL code:

from Django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path(r'admin/', admin.site.urls, name='admin' ),
    path(r'accounts/', include('django.contrib.auth.urls')),
    path(r'api-auth/', include('rest_framework.urls'), name='rest_framework'),
    path(r'', include('app.urls')),

My App URL code:

from django.urls import path, include
from . import views #function views
from django.contrib.auth.decorators import login_required, permission_required


from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'tank', views.TankViewSet)
router.register(r'room', views.RoomViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.

urlpatterns = [
    path(r'',login_required(views.index), name='index'),
    path (r'test/',views.Test),
    path(r'^Water/',login_required(views.Lquid), name='tank'),
    path(r'^Ambient/',login_required(views.Ambient), name='room'),
    path(r'Rest-api/', include(router.urls)),
   #path(r'admin/', admin.site.urls, name='admin' ), this somthing I had try
    ]

Basically, Django makes in a way that works and redirect to the URL link on any page

NAVBAR: Admin site

App URL: path(r'',login_required(views.index), name='index'),

With this two link it will redricet to the page you set.

Apprentice if you cold help

like image 239
Fool Baby Avatar asked Oct 14 '25 18:10

Fool Baby


1 Answers

You need to specify the admin page you want to redirect to when doing a reverse lookup of admin. To get to the admin homepage you will want this reverse lookup

reverse('admin:index')

So to link to the admin homepage in your nav bar, it should look like this

<a class="nav-link" href="{% url 'admin:index' %}"> Admin site </a>

Check out the docs on how to reverse lookup admin pages

like image 174
Mikey Lockwood Avatar answered Oct 17 '25 09:10

Mikey Lockwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!