Package: django-mobileadmin
ContentsDownload & InstallationUsageExtendingmobileadminCreating a custommobileadminsetupUsing custom mobile admin interfacesMedia pathServingmobileadmin’s static mediaSupportThis is an alternative admin interface for Django for use with mobile devices
such as the iPhone/iPod touch or Blackberry. Some would call it a theme or a
skin, but actually it’s more than that.It resembles almost all features of the regular Django admin interface and
brings everything you need to add support for arbitrary devices.I hope you like it.Download & InstallationGet the source from the application site at:http://code.google.com/p/django-mobileadmin/To install themobileadmin, follow these steps:Follow the instructions in the INSTALL.txt fileAdd'mobileadmin'to theINSTALLED_APPSsetting of your Django site.
That might look like this:INSTALLED_APPS = (
   # ...
   'mobileadmin',
)Make sure you’veinstalledthe admin contrib app.Add'mobileadmin.context_processors.user_agent'to yourTEMPLATE_CONTEXT_PROCESSORSsetting. It should look like this:TEMPLATE_CONTEXT_PROCESSORS = (
   'django.core.context_processors.auth',
   'django.core.context_processors.debug',
   'django.core.context_processors.i18n',
   'django.core.context_processors.media',
   'django.core.context_processors.request',
   'mobileadmin.context_processors.user_agent',
)UsageSincemobileadminfollows the ideas of Django’s admin interface you can
easily reuse your existing admin setup:You use the default or custom ModelAdmin classes for each model you wanted
to be editable in the admin interface and hooked up Django’s default
AdminSite instance with your URLconf.If that’s the case you are able to re-use those ModelAdmin (sub-)classes
by usingmobileadmin’s separate admin site instance and its autoregister()
function.In your root URLconf – justbelowthe line where Django’s
admin.autodiscover() gets called – importmobileadminand call the
functionmobileadmin.autoregister():# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

import mobileadmin
mobileadmin.autoregister()

urlpatterns = patterns('',
    ('^admin/(.*)', admin.site.root),
)This registers your existing admin configuration withmobileadmin.Extend the urlpatterns to hook the defaultMobileAdminSiteinstance`
with your favorite URL, e.g./ma/. Add:urlpatterns += patterns('',
    (r'^ma/(.*)', mobileadmin.sites.site.root),
)mobileadminis now replicating all of the regular admin features and
uses templates adapted to the mobile device you are using.Set thehandler404andhandler500variables in your URLConf to the
views that are provided bymobileadmin:handler404 = 'mobileadmin.views.page_not_found'
handler500 = 'mobileadmin.views.server_error'That is needed to load the404.htmland500.htmltemplates
according to the user agent of the browser on your mobile device. It
has an automatic fallback toDjango’s default error handlers.Extendingmobileadminmobileadmincomes with a default set of templates and patterns to
recognize different devices and platforms:Mobile Safari (iPhone/iPod touch)But it’s made for being extended.Since the template loading depends on the user agent of the requesting client,mobileadmintells Django to look for three templates, when trying to render
one of the default admin views:mobileadmin/USER_AGENT_NAME/VIEW_TEMPLATE.htmlmobileadmin/VIEW_TEMPLATE.htmladmin/index.html..where:USER_AGENTis the short name of the user agentVIEW_TEMPLATEis the name of the rendered templateIf you would try to access the login view with the iPhone for example, the
following three templates would be tried to load:mobileadmin/mobile_safari/login.htmlmobileadmin/login.htmladmin/index.html..wheremobile_safariis the name of one of the default device patterns
andlogin.htmlthe name of the to needed template.Creating a custommobileadminsetupYou can add support for more user agents by addingMOBILEADMIN_USER_AGENTSto your settings.py file – consisting of a short name and a regualar
expression, matching that user agent string:MOBILEADMIN_USER_AGENTS = {
    'my_user_agent': r'.*MyUserAgent.*',
}With that it would automatically check if the regular expression matches with
the user agent of the current request and – if yes – try to load the
templatesmobileadmin/my_user_agent/login.html, when accessing the the
login page – falling back tomy_user_agent/login.htmland later toadmin/login.html, if not found.Have a look atTEMPLATE_MAPPINGinmobileadmin/conf/settings.pyif you want to know the default regular expressions.mobileadmincomes with aMobileAdminSiteand aMobileModelAdminclass that uses the defaultTEMPLATE_MAPPINGandUSER_AGENTSsettings out of the box:from mobileadmin import sites

class MyMobileAdminSite(sites.MobileAdminSite):
    # define here whatever function you want
    passBut if you want to use the ability ofmobileadminto change the template
depending on the user agent, you need to modify a bit of your admin classes.Luckilymobileadmincomes with a decorator to be used onAdminSiteorModelAdminmethods that changes the template of that method according to
the current user agent by using a template mapping, which can be found inmobileadmin/conf/settings.pyin theTEMPLATE_MAPPINGvariable.Those mappings are used by the decoratormobile_templatesthat applies
them on the corresponding methods of your ownAdminSiteorModelAdmin, e.g.:from django.contrib.admin import sites
from mobileadmin.decorators import mobile_templates

class MyAdminSite(sites.AdminSite):

    def index(self, request, extra_context=None):

        # self.index_template is already automatically set here
        # do something cool here

        return super(MyAdminSite, self).index(request, extra_context)
    index = mobile_templates(index)Furthermore the default mappings can be extended in your site settings.py:MOBILEADMIN_TEMPLATE_MAPPING = {
    'index': ('index_template', 'index.html'),
}..where:indexis the name of the function, whose class attribute andindex_template(an attribute of the method’s class) would be set to the
the fileindex.html.Using custom mobile admin interfacesIn case you created your own mobile admin interface, you can usemobileadmin’s subclasses of Django’sModelAdmin,InlineModelAdminandAdminSiteclasses, that include the neccesary bits to make it work.Just use it as you would use the base classes, e.g.:from mobileadmin import options
from myproject.myapp.models import Author

class MobileAuthorAdmin(options.MobileModelAdmin):
    pass
mobileadmin.sites.site.register(Author, MobileAuthorAdmin)Then importmobileadminin your URLconf to instantiate aMobileAdminSiteobject, use Django’sautodiscover()to loadINSTALLED_APPSadmin.py modules and add an URL for themobileadminto
the URLConf:# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
import mobileadmin

admin.autodiscover()

urlpatterns = patterns('',
    ('^admin/(.*)', admin.site.root),
    (r'^ma/(.*)', mobileadmin.sites.site.root),
)Media pathPlease feel free to use some nice little helpers to find the path tomobileadmin’s media directory. If you are using Django (or any other Python
software) to serve static files (which you shouldn’t in production) just use
for example:from mobileadmin.conf import settings

mobileadmin_media_path = settings.MEDIA_PATH
mobileadmin_media_prefix = settings.MEDIA_PREFIXYou now have the full (platform-independent) path to the media directory ofmobileadminin the variablemobileadmin_media_pathand the default URL
prefix ('/mobileadmin_media/') for themobileadminmedia – CSS, Javascript
and images – inmobileadmin_media_prefix. Just like theADMIN_MEDIA_PREFIXbut for themediadirectory of themobileadminapp.You can of course optionally override the default URL prefix by setting
aMOBILEADMIN_MEDIA_PREFIXin the settings.py file of your Django site.
Please use a trailing slash. This makes updatingmobileadminmuch easier for
you, since you now don’t have to bother about different locations of the media
directory.Servingmobileadmin’s static mediaEven though using Django’s ability to serve static files is stronglyNOT
RECOMMENDEDfor live production servers, it might be helpful to bring upmobileadminfor a test drive or an intranet website. Just add the following
code to the URLConf (urls.py) of your Django site:from mobileadmin.conf import settings

urlpatterns += patterns('django.views.static',
    (settings.MEDIA_REGEX, 'serve', {'document_root': settings.MEDIA_PATH}),
)See howmobileadmin’s own settings module is loaded at the top of the snippet
that enables you to obtain a ready-made regex for the static files
(MEDIA_REGEX) and the platform-independent filesystem path to the media
files (MEDIA_PATH).SupportPlease leave yourquestions and problemson thedesignated Google Code site.