Upgrade to Pro — share decks privately, control downloads, hide ads and more …

15-437 Admin

ThierrySans
September 24, 2013

15-437 Admin

ThierrySans

September 24, 2013
Tweet

More Decks by ThierrySans

Other Decks in Education

Transcript

  1. A blog web application We need to have an interface

    to • post, delete and modify articles • moderate comments • manage authorized writers and viewers ➡ We need an administration interface
  2. Why having an administration interface Inspecting data
 see what the

    database contains Managing acquired data
 insert, modify and delete database records Managing users
 define who can access your application ✓ All of these should be accessible for non technical users

  3. How to build such an administrator interface? ➡ We could

    build the administrator interface from scratch ๏ It's painfully, boring and repetitive to build it ✓ With Django “batteries are included”
  4. Step 1 - The project settings (done in 1.7) setting.py

    ...! INSTALLED_APPS = (! 'django.contrib.admin',! 'django.contrib.auth',! 'django.contrib.contenttypes',! 'django.contrib.sessions',! 'django.contrib.messages',! 'django.contrib.staticfiles',! 'helloyou',! 'WebDirectory',) ...
  5. Step 2 - The URL Dispatcher (done in 1.7) from

    django.conf.urls import patterns, include, url! from django.contrib import admin
 ! urlpatterns = patterns(‘',! ! url(r'^admin/', include(admin.site.urls)),! ! url(r'^helloyou/', include('HelloYou.urls')),! ! url(r'^webdirectory/', include('WebDirectory.urls')),! )! ! tsans/urls.py
  6. Step 2 - Modify admin.py from django.contrib import admin! !

    from WebDirectory.models import Entry, Image!! ! ! ! ! ! ! admin.site.register(Entry)! admin.site.register(Image) WebDirectory/admin.py makes Entry and Image data models available on the admin interface
  7. From WebDirectory admin you can get the list of records

    modify records delete records add records
  8. The superuser administrator has all powers • Create, modify and

    delete users • Insert, modify, delete records from every application
  9. How to be more restrictive? ✓ We want to create

    an administrator for the WebDirectory application only • A dedicated user admin named John Doe - login: jdoe • With the administrative privileges to create, modify and delete records in WebDirectory only
  10. What the administrator can do ... John Doe can administrate

    WebDirectory only After logging in as jdoe ...
  11. How to customize the admin interface? ✓ By editing the

    admin.py • The class ModelAdmin is the representation of the models in the admin interface • Changing the admin options 1.create a new model admin object
 class EntryAdmin(admin.ModelAdmin):
 ...! 2.register this new admin object 
 admin.site.register(Entry,EntryAdmin)
  12. from WebDirectory.models import Entry! from django.contrib import admin! ! !

    ! ! ! ! ! ! class EntryAdmin(admin.ModelAdmin):! fields = [(‘name', ‘webpage’)]! ! admin.site.register(Entry,EntryAdmin) WebDirectory/admin.py
  13. from WebDirectory.models import Entry! from django.contrib import admin! ! !

    ! ! ! ! ! ! class EntryAdmin(admin.ModelAdmin):! fields = [(‘name', ‘webpage’)]! ordering = ('name',)! search_fields = ['name']!! ! ! ! admin.site.register(Entry, EntryAdmin) WebDirectory/admin.py
  14. WebDirectory/admin.py class ! ImageAdmin(admin.ModelAdmin):! list_display = ['entry','image_tag','mimeType']! list_filter = ['mimeType']!

    ! ! ! ! admin.site.register(Image, ImageAdmin) WebDirectory/model.py class ! Image(models.Model):! entry = models.ForeignKey(Entry, unique=True)! image = models.ImageField(upload_to='WebDirectory')! mimeType = models.CharField(max_length=20)
 ! def image_tag(self):! return u'<img src="%s" style="width:100px;height: 100px;" />' % reverse('getImage', kwargs={'image_id': self.id})! image_tag.short_description = 'image'! image_tag.allow_tags = True
  15. More customization We should be able to change the entire

    page layout ✓ Redefine HTML and CSS of the admin pages ➡ Redefine (extends) the predefined admin templates
  16. Step 1 - Create an admin directory in the project

    tsans/! manage.py! tsans/! __init__.py! settings.py! urls.py! wsgi.py! templates/! admin/! WebDirectory/! !
  17. Step 2 - Copy the admin templates $ python! >>

    import django! >> django.__file__! '/usr/local/lib/python2.7/site-packages/django/__init__.pyc' • Find your Django installation path my django_path • Copy the template base_site.html
 from django_path/contrib/admin/templates/admin/
 to project_path/tsans/templates/admin/
  18. For example, change the title of the admin portal {%

    extends "admin/base.html" %}! ! {% block title %}{{ title }} | 
 {{ site_title|default:_('My Custom Admin Portal') }}{% endblock %}! ! {% block branding %}! <h1 id="site-name"><a href="{% url 'admin:index' %}">
 {{ site_header|default:_('My Custom Admin Portal') }}</a></h1>! {% endblock %}! ! {% block nav-global %}{% endblock %}! tsans/templates/admin/base_site.html title in the header title in the body