Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create custom page for django admin?

I want to create custom page for admin panel without model. For first i copy index.html to project folder:

mysite/     templates/         admin/             index.html 

Then add to apps block my code:

<div class="module">     <table summary="{% blocktrans with name="preferences" %}Models available in the preferences application.{% endblocktrans %}">         <caption><a href="preferences" class="section">{% blocktrans with name="preferences" %}Preferences{% endblocktrans %}</a></caption>             <tr>                 <th scope="row"><a href="preferences">Preferences</a></th>                 <td><a href="preferences" class="changelink">{% trans 'Change' %}</a></td>             </tr>     </table> </div> 

This works good, then I create new page /templates/admin/preferences/preferences.html and add to urls.py:

url(r'^admin/preferences/$', TemplateView.as_view(template_name='admin/preferences/preferences.html')), 

And add code to preferences.html:

{% extends "admin/base_site.html" %} {% block title %}Test page{% endblock %} 

Run it and see message with error "The requested admin page does not exist.". What I do wrong?

like image 326
Gr1N Avatar asked Apr 07 '12 11:04

Gr1N


People also ask

How do I get to the admin page in Django?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

How do I create an admin panel in python?

You should see a url http:127.0. 0.1:5000 provided to you in your terminal, open your preferred browser and navigate to that url or localhost:5000. You should see the login screen of the Admin Dashboard. To be able to access the main page/dashboard, you need to create an account.


2 Answers

You need to add your admin URL before the URL patterns of the admin itself:

urlpatterns = patterns('',    url(r'^admin/preferences/$', TemplateView.as_view(template_name='admin/preferences/preferences.html')),    url(r'^admin/', include('django.contrib.admin.urls')), ) 

This way the URL won't be processed by Django's admin.

like image 126
Simeon Visser Avatar answered Sep 23 '22 14:09

Simeon Visser


Years go by and still a relevant answer to this can be posted.

Using Django 1.10+ you can do:

security/admin.py (this is your app's admin file)

from django.contrib import admin from django.conf.urls import url from django.template.response import TemplateResponse from security.models import Security   @admin.register(Security) class SecurityAdmin(admin.ModelAdmin):      def get_urls(self):          # get the default urls         urls = super(SecurityAdmin, self).get_urls()          # define security urls         security_urls = [             url(r'^configuration/$', self.admin_site.admin_view(self.security_configuration))             # Add here more urls if you want following same logic         ]          # Make sure here you place your added urls first than the admin default urls         return security_urls + urls      # Your view definition fn     def security_configuration(self, request):         context = dict(             self.admin_site.each_context(request), # Include common variables for rendering the admin template.             something="test",         )         return TemplateResponse(request, "configuration.html", context) 

security/templates/configuration.html

{% extends "admin/base_site.html" %} {% block content %} ... {% endblock %} 

See Official ModelAdmin.get_urls description (make sure you select proper Django version, this code is valid for 1.10 above)

  • Note the use of get_urls() above.
  • This new admin page will be accessible under: https://localhost:8000/admin/security/configuration/
  • This page will be protected under admin login area
like image 22
Rui Carvalho Avatar answered Sep 22 '22 14:09

Rui Carvalho