How to bypass django admin index and go to a specific application page?

社会主义新天地 提交于 2020-01-06 18:12:34

问题


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?


回答1:


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",)))



回答2:


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/")



回答3:


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




回答4:


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)),
)


来源:https://stackoverflow.com/questions/12424000/how-to-bypass-django-admin-index-and-go-to-a-specific-application-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!