Can't create link to admin page in my template

Problem: Can't create link to admin page in my template. I have looked for solution, but can't find it. I have boiled problem down to most simple, and posted here...

Essentially, I'm just trying to create a link so i can just click it to run the admin.  I have created a simple example to show the problem, the same problem i am having in my big app.

I changed angle brackets in links to ( ) so they aren't real links, as I am a new user and this forum is limiting me as a new user... (and it doesn't convert the link actual link, so easier to see)

My Steps... note key '&&' below indicates code included:
==========================================
-Create basic proj and app1 then superuser account
-Login to 127.0.0.1:8000/admin (ok)
-Create view &&'home'
-Create templates folder and subfolder app1
-Create template home.html, and insert ONE LINE ONLY:
  (a href="{% url 'admin' %}">Admin</a)
  Angle brackets changed to ( ) so it shows in this post)
-Create &&apps1/urls.py
-Added app to settings.py
-Include above apps1/urls.py in &&proj/urls.py
-Bring up, browse to 127.0.0.1:8000/home and get the ##traceback

Here's the && enclosures / attachments...

view:
def home(request):
return render(request,"app1/home.html")

template:
(a href="{% url 'admin' %}">Admin</a)

proj urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls, name='admin'),
path("", include("app1.urls")),
]

app1 urls.py:
from django.contrib import admin
from django.urls import path
from app1 import views
urlpatterns = [
path('home/', views.home, name='home'),
]

traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/home/
Django Version: 4.0.3
Python Version: 3.9.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1']

Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'[django.middleware.common.CommonMiddleware](http://django.middleware.common.commonmiddleware/)',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template error:
In template /Users/Fred/batch/tempdjangoproject/mytempproj/app1/templates/app1/home.html, error at line 1

Reverse for 'admin' not found. 'admin' is not a valid view function or pattern name.
1 : (a href=" {% url 'admin' %} ">Admin</a)
Traceback (most recent call last):

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/Fred/batch/tempdjangoproject/mytempproj/app1/views.py", line 7, in home
return render(request,"app1/home.html")

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/shortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/backends/django.py", line 62, in render

return self.template.render(context)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/base.py", line 175, in render

return self._render(context)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/base.py", line 167, in _render

return self.nodelist.render(context)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/base.py", line 1000, in render

return SafeString("".join([node.render_annotated(context) for node in self]))

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/base.py", line 1000, in <listcomp>

return SafeString("".join([node.render_annotated(context) for node in self]))

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/base.py", line 958, in render_annotated

return self.render(context)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/template/defaulttags.py", line 472, in render

url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/urls/base.py", line 88, in reverse

return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)

File "/Users/Fred/batch/tempdjangoproject/venv/lib/python3.9/site-packages/django/urls/resolvers.py", line 802, in _reverse_with_prefix

raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /home/

Exception Value: Reverse for 'admin' not found. 'admin' is not a valid view function or pattern name.

Your error message is saying the error is in your template:

It would have been helpful to see the template. We would have been able to provide a more specific response.

But, working from the information presented, admin urls are dynamically generated from your models and other configurations. The name you tried to use in your urls isn’t available. Try reversing ‘admin:index’.

Side Note: When posting code or errors or tracebacks or templates here, enclose each block of text between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or template, etc), then another line of ```. This forces the forum software to keep your text properly formatted and prevents it from interpreting tags in the text that would otherwise reformat your posting.
(And, you can go back and edit your original post to put lines of ``` before and after your code.)

1 Like

Hi Ken, I hope it’s more readable now (of course I couldn’t see my post to see the mess it was until my account was approved). The template just has ONE line in it, so probably no need to add it as a separate entity.
/fj

You need to use the backtick - ` character, not the apostrophe - '.

But yes, on that line, you need to change ‘admin’ to ‘admin:index’. Your name assignment on the admin path entry in your urls doesn’t apply here.

oops SORRY, you were very clear on that LoL.
I’ll fix again…
(hope it’s ok to just do the whole thing in ```)
/fj

That’s fine for this post, don’t bother with a third edit. Just keep it in mind for future posts that it does end up being easier to read if only the code sections are so enclosed, allowing you to use the other markup on the rest of your text.

1 Like

Thanks Ken, changing
<a href="{% url 'admin' %}">Admin</a>
to
<a href="{% url 'admin:index' %}">Admin</a>
did the trick…

HOW would I figure that out? (what were the steps you took?)

Thanks!!

When in doubt, go to the source.

If you notice, the admin url specification is different than the other specifications you use.

It’s not using the include function, and it’s not the name of a view - but it is the name of a function in django.contrib.admin.sites. That function returns the values of the get_urls function, the literal string “admin”, and a class attribute self.name.

If you then look at the get_urls function in that same class, you’ll see that it has a urlpattern definition, with an empty url, using the name index.

I guessed that that would be the name assigned to that app’s entry, and tried running the reverse function with it in the Django shell. (e.g. reverse('admin:index')) When I got the expected result, I felt confident that my guess was correct. I then put that line into a template into my test system to verify.

Thanks!
So if i then hide my admin by changing the urls.py from:
path('admin/', admin.site.urls),
to
path('hidemyadmin/', admin.site.urls),
what would be the correct reverse for that? (or correct way to hide /admin)?
/fj

You wouldn’t need to change anything. The app name is still admin, even though it’s being referenced by a different url. You would still reverse admin:index.

Thanks, and thanks for being so patient with me…
(Like soooo many in the Django community) !
/fj