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?
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).
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With