На Fri, 2 Sep 2011 10:24:03 +0300
Ivan Ivanov <wankata@openintegra.com> написа:
> Hallo everybody!
>
> I've got problem exteding the admin's index view. I need to pass
> extra_context to the index of the admin, after the administrator
> logged in, that's why I'm trying to extend the view. So...
>
> What I've got is:
>
> In the root of the project I modified urls.py like following:
> (r'^admin/$', project.admin.admin_site.index),
> (r'^admin/', include(admin.site.urls)),
This one here must be
# the first one is the magic
(r'^admin/', project.admin.admin_site.urls),
(r'^admin/', include(admin.site.urls)),
You'll find out why just a little bit later.
>
> Again in the root of the project I've got admin.py with the following
> snippet of code:
>
> [...]
> class AdminSiteRegistryFix( object ):
> '''
> This fix links the '_registry' property to the orginal AdminSites
> '_registry' property. This is necessary, because of the character
> of the admins 'autodiscover' function. Otherwise the admin site will
> say, that you havn't permission to edit anything.
> '''
>
> def _registry_getter(self):
> return default_site._registry
>
> def _registry_setter(self,value):
> default_site._registry = value
>
> _registry = property(_registry_getter, _registry_setter)
>
>
> class MyAdmin(sites.AdminSite, AdminSiteRegistryFix):
> @never_cache
> def index(self, request, extra_context={}):
> last_report_date = models.Reports.objects.latest().entry_date
> now = datetime.now()
> delta = now - last_report_date
> extra_context['last_report_interval'] = delta.days
>
> return super(MyAdmin, self).index(request, extra_context)
>
Here we add another two functions:
def get_urls(self):
from django.conf.urls.defaults import patterns, url
urls = super(MyAdmin, self).get_urls()
my_urls = patterns('',
(r'^admin/$', self.admin_view(self.index))
)
return my_urls+urls
The first one adds our url pattern to the admin urls. It's important to
use admin_view as wrapper for self.index – that was actually the
problem with the login. I haven't wrapped my view with admin_view.
To get this function called, you need to write function with property
decorator, which returns get_urls and... (see below). Actually that was
the other big problem – I tried to set property like that: urls =
property(get_urls) but it doesn't work, because get_urls returns only
the list with the urls, but no app_name and name.
@property
def urls(self):
return self.get_urls(), self.app_name, self.name
And so it works.
> admin_site = MyAdmin()
>
>
> And it works. I've got the last_report_interval in the index.html
> template and I can write my lovely message to the admin. The problem
> is, that the ^admin/$ address swiches between the login and index
> view, despite of users authentication status. And this is nice, but
> not working after my changes, written above. Now I see only the app
> list of the index. When I'm not logged in, I just see an empty list,
> but I've got no login form...
>
> Can anyone help me, understanding why has my login form disappeared?
>
> What I can see from the django.contrib.admin.sites is that the
> admin_view function is the one, calling the login view. But I have
> noting to do with it, I haven't modify it and I don't understand what
> disturbs it's functionallity.
>
> Thank you in advance for your help!
>
> Ivan Ivanov
>
>
>
>
>
>
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment