How to bypass django admin index and go to a speci

2019-08-17 01:36发布

I've got a Django project with only one app.

I don't need the site admin (same as my app admin), so I would like to bypass the site admin and go directly to my app admin, i.e. go directly to mysite/admin/myapp/ after loging in and removing Home in the breadcrumb.

How can I do that?

4条回答
Deceive 欺骗
2楼-- · 2019-08-17 02:05

What you can do is override the admin index page to redirect to app specific admin page.

As stated by @FallenAngel, you need to update the urls.py to have your view for admin index page. That view can redirect to inner app admin page.

from django.http import HttpResponseRedirect
from django.core import urlresolvers
def home(request):
    return HttpResponseRedirect(urlresolvers.reverse('admin:app_list', args=("myapp",)))
查看更多
甜甜的少女心
3楼-- · 2019-08-17 02:07

Django Url Dispatcher scans all defined url definitions and redirect the request to the first matching url definition. At this point, url ordering is important, in urls.py file of your project root:

urlpatterns += patterns('',   
    ...
    (r'^admin/', include(admin.site.urls)),
    ...
)

You can define an url redirection form the admin index page url to a custom view

urlpatterns += patterns('',   
    ...
    (r'^admin/$', 'myapp.views.home'),
    (r'^admin/', include(admin.site.urls)),
    ...
)

So, if the request url is your admin index page, then your custom view will be called, if it is an admin page (that starts with admin/) but not admin index page, then admin.site.urls will be executed...

in your myapp.views write a simple redirection view:

from django.http import HttpResponseRedirect

def home(request):
    return HttpResponseRedirect("/myapp/")
查看更多
冷血范
4楼-- · 2019-08-17 02:17

You can comment it out in the INSTALLED_APPS if you don't want that in your project. Just to remove it from admin you need to unregister. Check it out here.

Another hack is, you can override the admin url mysite/admin/ to simply redirect to mysite/admin/myapp/ in which you don't need to define your own redirect view. Django DRY rocks there. :)

from django.http import HttpResponseRedirect

urlpatterns = patterns('',
                       url(r'^mysite/admin/$', lambda x: HttpResponseRedirect('/mysite/admin/myapp/')),
                       url(r'^mysite/admin/', include(admin.site.urls)),
                      )

For more customization you need to change the admin index page by customizing the admin templates. Doc is here. https://docs.djangoproject.com/en/1.3/intro/tutorial02/#customize-the-admin-index-page

查看更多
时光不老,我们不散
5楼-- · 2019-08-17 02:26

The cleanest and easiest way is to use the generic RedirectView in urls.py:

from django.core.urlresolvers import reverse_lazy
from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^admin/$', RedirectView.as_view(url=reverse_lazy('admin:app_list',
        args=('myapp',)))),
    url(r'^admin/', include(admin.site.urls)),
)
查看更多
登录 后发表回答