When you extend AdminSite to create another admin site how do you go about being able to reverse match each site? It seems the admin namespace is hardcoded reverse('admin:index'), is there a way to supply a custom namespace?
You may be confused with the namespace in django. If you are interested to clarify that confusion, you may read up the discussion here.
If you want to try solve your problem, there is a specific documentation for multiple admin sites.
Below are example solutions mostly copied from official documentation
Example Solution:
# urls.py
from django.conf.urls import url
from .sites import basic_site, advanced_site
urlpatterns = [
url(r'^basic-admin/', basic_site.urls),
url(r'^advanced-admin/', advanced_site.urls),
]
And
# sites.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
site_header = 'Monty Python administration'
basic_site = MyAdminSite(name='myadminbasic')
advanced_site = MyAdminSite(name='myadminadvanced')
Reversing
reverse('myadminbasic:index') # /basic-admin/
reverse('myadminadvanced:index') # /advanced-admin/
Related
What is the simplest way to add a view to my Django (3.2) admin UI? That is, add a URL mysite.com/admin/my-view so that visiting that URL acts like the rest of admin (in particular, requires similar permissions).
There is a whole page on this, but it's not obvious to me how to piece it together.
https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#adding-views-to-admin-sites says you can add a get_urls to your AdminSite class. Okay, so I need my own AdminSite class. And I need to register it in apps, maybe?
I did this:
class MyAdminSite(admin.AdminSite):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path('my-view', self.my_view, name='my-view')
]
return my_urls + urls
def my_view(self, request):
# do something ..
admin_site = MyAdminSite(name='my_admin')
and this in urls.py:
from .admin import admin_site
urlpatterns = [
path('admin/', admin_site.urls),
and this in unchanged in INSTALLED_APPS in settings:
'django.contrib.admin',
But, now it only shows the admin for the one app, instead of for all the apps. So how do I get it to auto-discover all the apps like it used to? Or is there a simpler way?
P.S. There is also a question on this, but the answer didn't have enough detail for me to use it.
EDIT: I'm reading Make new custom view at django admin and Django (1.10) override AdminSite ..
EDIT 2: This was a hack, but it works for me:
from django.contrib.admin import site
admin_site._registry.update(site._registry)
I had to update in the right place (the project urls.py), perhaps when all the other admin stuff is already loaded.
I'm setting the docs for my api and when I ask for (http://localhost:8000/api/v0/docs/) The app show me this error:
NoReverseMatch at /api/v0/docs/
'api-docs' is not a registered namespace
when try to do this
<script src="{% url **'api-docs:schema-js'** %}"></script>
this is my urls.py file:
from django.contrib import admin
from django.conf.urls import url, include
from django.contrib.auth.models import User
from . import views
from rest_framework import routers, serializers, viewsets
from rest_framework.documentation import include_docs_urls
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'goals', views.GoalViewSet)
urlpatterns = [
url(r'^goals/$', include(router.urls)),
url(r'^docs/', include_docs_urls(title='My API title')),
]
I added the namespace to include_docs_urls but it doesn't work. (I have installed coreapi, pygments and markdown).
Beforehand thanks for helping me.
You should move your docs url pattern into your main urls.py file. This is still a problem in DRF, there is also an issue opened on the official GitHub source: https://github.com/encode/django-rest-framework/issues/4984
The official developer of Django REST Framework, Tom Christie, says:
It's important enough that I'd like to see it highly prioritized, but there's a lot of other things going on at the moment too. Even after we release 3.6.3, there's still also all the work towards 3.7.0's realtime integration.
So your docs pattern should not have any prefixes (like api/v0/).
In conclusion, change your main urls.py:
url(r'^docs/', include_docs_urls(title='API Title'))
FIRST:
I used django admin site as my website management system,but some features are not appropriate to me,so i decide to make my own,according to django docs,what i learned is that I can override Templates per app or model while i can extend AdminSite to build a brand new one.But,i have some problems playing around them,what i was trying to do were:
1,article/admin.py (one app of my project)
from django.contrib import admin
from django.contrib.admin import AdminSite
from .models import Article,ArticleImage,Category
class MyAdminSite(AdminSite):
site_header = "Moonmoonbird administration"
admin_site = MyAdminSite(name="myadmin")
admin_site.register(Article)
2,settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_swagger',
#'django.contrib.admin',
'django.contrib.admin.apps.SimpleAdminConfig',(which docs suggest to do)
'jwtauth',
'navigation',
'myadmin',
'article',
)
3,urls.py
from django.conf.urls import patterns, include, url
from jwtauth.views import obtain_jwt_token
from django.contrib.auth.admin import admin
from article.admin import admin_site
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'moonmoonbird.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'admin/',include(admin.site.urls)),
url(r'myadmin/',include(admin_site.urls)),
url(r'^auth/',obtain_jwt_token.as_view())
)
When i access example.com/admin and example.com/myadmin,they are the same,both are the django admin site default implementation.I cannot see where i was doing wrong.
SECOND:
Lets say a use case(which i meet now),an article app of my project,which has title,tag,and content attributes,I install it into admin site by doing this:
article/admin.py:
from django.contrib import admin
admin.site.register(Article)
admin.site.register(Category)
admin.site.register(ArticleImage)
and now i can manipulate CURD in admin site,but what i want is that i can edit my article content like SO do,that is using a pagedown editor for say(which actually i want to use) ,but i dont know how to do it.
Question:
1, how can i make the FIRST workflow works?
2,how to do and what you suggest me to do to implement the SECOND case?
Thanks in advance!
Think well if you really have to change the whole admin template. In majority of cases, you need several small changes. There are good properties in admin.ModelAdmin class for many useful customizations. It is even possible to override some parts of the admin template with your HTML template.
Read the Django tutorial, Part 2 for examples how to easily change Admin page functionality and look.
Can you tell what exactly you need to change in your admin page?
In Django, is it possible to have two different files with url patterns, neither of which is called urls.py ? Or does Django rely on there being only one set of url patterns per Django app, and that it must be called urls.py ?
I'm using Django CMS and I want to split an app across two apphooks and two menus. So I've tried splitting urls.py into pub_urls.py and train_urls.py but I appear to have broken things by doing that, despite the cms_app.py naming the correct urls - eg:
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
from resources.menu import TrainingMenu, PublicationMenu
class PublicationApp(CMSApp):
name = _("Publication App") # give your app a name, this is required
urls = ["resources.pub_urls"] # link your app to url configuration(s)
menus = [PublicationMenu]
class TrainingApp(CMSApp):
name = _("Training App") # give your app a name, this is required
urls = ["resources.train_urls"] # link your app to url configuration(s)
menus = [TrainingMenu]
apphook_pool.register(PublicationApp) # register your app
apphook_pool.register(TrainingApp) # register your app
Is something like this possible? Or do I have to split this into two different apps?
There is nothing to stop your urls.py simply acting as a way of including multiple other urls files:
urls.py:
from django.conf.urls.defaults import patterns, include
urlpatterns = urlpatterns + patterns('',
(r'^', include('pub_urls')),
(r'^', include('train_urls')))
pub_urls.py:
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('',
(r'^$', 'homeview'),
(r'^stuff/$', 'stuffview')
)
etc.
ROOT_URLCONF in your settings file points to the root url file.
Django doesn't care what your urlpatterns file is called. The default base urlconf is by convention called urls.py, but in fact that's just a setting and can be overridden. After that, you need to explicitly include urlconfs by module name, so again it makes no difference what they're called.
I'm not familiar with Django-CMS and I don't know what it's doing in its CMSApp class, but I suspect you're going to have to dig into that to see what's going on.
It is configurable using ROOT_URLCONF setting.
From django docs
ROOT_URLCONF
A string representing the full Python import path to your root URLconf.
For example: "mydjangoapps.urls". Can be overridden on a per-request basis
by setting the attribute urlconf on the incoming HttpRequest object. See How
Django processes a request for details.
You can also write/get a middleware which can set it appropriately depending upon the host or other parameters in the request.
In DjangoBook, it says that the every books app should have its own admin.py. However, none of my apps have there own separate admin.py as the text suggests. I was just wondering if this is a Django 1.3 thing and if so, where is the admin.py data stored now, if not in a separate admin.py file?
The chapter I'm referring to is here:
http://djangobook.com/en/2.0/chapter06/
P.S. I'm not talking about django-admin.py
By default there is no admin.py file created for you when you create a new app, you will need to create your own. Here are the directions on how to create the admin.py file.
http://docs.djangoproject.com/en/1.3/ref/contrib/admin/
Edit: 1.3 is no longer supported, here is a link to 1.8:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/
You need to create admin.py in new app and edit it with from django.contrib import adminIn project/urls.py, in my case (mysite/urls.py) specify the url of newapp/admin (polls/admin) likefrom django.conf.urls import patterns, include, url from django.contrib import admin
admin.autodiscover() urlpatterns = patterns('',url(r'^admin/', include(admin.site.urls)),)