Code

Changes between Version 23 and Version 24 of NewformsHOWTO


Ignore:
Timestamp:
03/07/08 01:41:37 (6 years ago)
Author:
Lllama <f.ingram.lists@…>
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NewformsHOWTO

    v23 v24  
    577577== Q: How can I pass extra context variables into my index page? == 
    578578 
    579 You can't. At least not in the same way as above. The index page has a fairly 
    580 simple view, so you should just recreate it and add in the extra variables you need. 
    581 You'll also need to make sure that you override the approriate URL in urls.py 
    582 Here's an example: 
    583 {{{ 
    584 #!python 
    585 from django.contrib.admin.views.decorators import staff_member_required 
    586 from django.views.decorators.cache import never_cache 
    587 from django.utils.translation import ugettext 
    588 from django.shortcuts import render_to_response 
    589 from django import template 
    590 import subprocess 
    591  
    592 def new_admin_index(request): 
    593     # Here's our extra variable - output from the fortune package. 
    594     fortune = subprocess.Popen("/usr/games/fortune -s", shell=True, stdout=subprocess.PIPE).communicate()[0] 
    595  
    596     return render_to_response('admin/index.html', {'fortune': fortune, 'title': ugettext('Site administration')}, context_instance=template.RequestContext(request)) 
    597 new_admin_index = staff_member_required(never_cache(new_admin_index)) 
    598 }}} 
    599  
    600 Here's the overriding in urls.py 
    601 {{{ 
    602 #!python 
    603 from your_project.extras import new_admin_index 
     579You can't. At least not in the same way as above. 
     580 
     581There are two ways you can do this at present, the messy way or the 'proper' way. 
     582 
     583The messy way is as follows. 
     584Create a file called extras.py in your project's root directory (where urls.py lives). 
     585In it create a copy of the index method from django.contrib.admin.sites (from the class AdminSite). 
     586Modify the method to add in your extra context variables. 
     587 
     588In your urls.py import your new method and the 'new' module. 
     589{{{ 
     590#!python 
     591from django.contrib import admin 
     592from your_project.extras import new_index_method 
     593import new 
     594}}} 
     595 
     596Now you'll need to replace the existing index method with your new one 
     597{{{ 
     598#!python 
     599admin.site.index = new.instancemethod(new_index_method) 
     600}}} 
     601 
     602this isn't that pretty but it works. 
     603 
     604The 'proper' way will be to create your own AdminSite instance and register your models to it. The problem with this method is that you will also need to register all of the models from the auth app if you want to manage your users. 
     605 
     606{{{ 
     607#!python 
     608from your_project.extras import my_admin_site_instance 
    604609... 
    605610urlpatterns = patterns('', 
    606611    # Override index page to add in fortune output 
    607     (SITE_BASE+r'/$', new_admin_index), 
     612    (SITE_BASE+r'/(.*)', my_admin_site_instance.root), 
    608613... 
    609614}}}