Creating A Button For The Admin Page

Hi everyone.

I am trying to create a button that will take the user to the ‘http://127.0.0.1:8000/admin/’ URL.

Inside my django_project -> django_project -> urls.py file I have the following:

urlpatterns = [
    path('admin/', admin.site.urls, name='admin'),
]

However, for the following button inside django_project -> users -> templates -> users -> FacialLoginResult.html, I am getting an error message saying " Reverse for ‘admin’ not found. ‘admin’ is not a valid view function or pattern name.":

    <a class="button" href="{% url 'admin' %}">Proceed</a>

How can I create a URL name for the ‘http://127.0.0.1:8000/admin/’ page and use it in a button without seeing this error? What is the correct approach to take? Thank you.

The path for admin is not a reference to a page or an included urlpatterns - it’s kinda a unique case. Anyway, the name parameter doesn’t seem to apply to anything here.

If you look at the source for the Django admin site, the root url has a name defined - "index" in the app named admin. Therefore, the url reference to it would be 'admin:index'. (This also still works if you rename your admin site to a different name for security purposes. For example, if you define it as path('xyzzy/', admin.site.urls), then reverse('admin:index') will return '/xyzzy/'.)

Thank you very much Mr. Whitesell! The button is now working and directs the user to the admin’s page.

How do you think I can solve the following issue.

The following is a button that I have where I am trying to have the user click the button and open the link in another tab:

<button onclick="window.open("{% url 'profile' %}", '_blank'); return false;"
        class="btn btn-default" data-dismiss="modal">Go To The Profile Page</button>

href="{% url 'profile' %}" is how I normally format the URL name, but how would I format the URL name in this case as the first parameter of the window.open() function? The first parameter of the window.open() function expects a string indicating the URL.

Why do you think the reference here would be different from any other location in the template?

Because I get an error when trying it to do it the following way:

<button onclick="window.open("{% url 'profile' %}", '_blank'); return false;"
        class="btn btn-default" data-dismiss="modal">Go To The Profile Page</button>

The following is an image of the error:

Ultimately, the error is saying the following:

')' expected.javascript

How can I fix this issue?

Ok, the issue here is that the quote before the { is closing the quotes in the onclick definition. So the problem here isn’t the use of the url tag, it’s the nested quotes within the JavaScript fragment.

It might be easiest to assign the url to a variable and reference that variable in the call to window.open.

You mean assign the URL (“{% url ‘profile’ %}”) to a JavaScript variable in the <script></script> section of the code?

Yes, that should allow you to get around this specific issue.

Good news Mr. Whitesell! I got the button to work and now the following link open in another tab http://127.0.0.1:8000/profile/.

The following is what I have:

<html>
<body>
<button onclick="window.open(profilePage, '_blank'); return false;"
                    class="btn btn-default" data-dismiss="modal">Go To The Profile Page</button>
</body>

<script>
    let profilePage = "{% url 'profile' %}";
</script>
</html>

My question is the following: Am I doing this the correct way? Would you do it a different way?

Thank you very much indeed Mr. Whitesell!

I would say that you are doing it one of the correct ways. I wouldn’t say this is the only way to do it - there are other ways of handling this that are just as valid.

Would I do it a different way? For me, it depends. There’s the possibility that other circumstances or conditions that may exist where I’d find it beneficial to choose a different approach. But that doesn’t make this “better” or “worse” than the other choices, just different.