2013年9月25日星期三

Crusher Django Tutorial(5) Using The Admin System

Cute and hate ADs: A website I made for sale impact-crusher(For god sake help me click it or just ignore it.55555555)

In the previous post,we learn the basic things about the Model,and we created an easy App -- TomAndJerry,in this tutorial we will continue the tutorial and use the Admin System in our TomAndJerry App,so let move on to the basic Admin System to manage our data -- the roles in the show.

First, we have to conf the url of admin:

 
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from django.conf.urls.defaults import *
from hello.TomAndJerry.views import *  # import all the funtions, we have only one

from django.contrib import admin   # import admin
admin.autodiscover()               # add this to make admin discover

urlpatterns = patterns('',
    (r'^$', show),  # conf the function
    (r'^just_show/$', just_show),
    (r'^info/(.+)/', info),
    (r'^info/$', info),
    (r'^admin/', include(admin.site.urls)), # add this line to conf admin page url
)
 

Then, edit the settings.py to tell Django we have the App of Admin:

 
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'hello.TomAndJerry',
    'django.contrib.admin', # Add the Admin App
)
 

Now, we have to use the python manage.py syncdb command to create the admin log otherwise we can not log in the admin page:

 
zoo@ubuntu:~/hello$ python manage.py syncdb
Creating table django_admin_log
Installing index for admin.LogEntry model
 

The command of python manage.py syncdb means that when we have anything new in the database we have to re-type it so that our table in database will re-new the model we created.

And now, we new a new python file in the folder TomAndJerry called admin.py which is the views of the admin page, when we log in the admin system, we can contorl the data with the player model:

 
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from django.contrib import admin
from hello.TomAndJerry.models import player

admin.site.register(player) # registe the player class 
 

Now, we re-start the Django server and just test our admin System:

Just add a new player named Fat-Dog who is 9 years old.

Now,we have 3 players in our database,just check the just_show page see if it change:

Great, our job works,that is our basic things about the admin system in Django, and next post we will learn how to use the static file like css, javascript and images in Django. You must don't want to miss it. Welcome to feedback.

没有评论:

发表评论