初学python的Web框架Django之二-后台管理
一 激活管理界面 Activate the admin site
1 Add “django.contrib.admin” to your INSTALLED_APPS setting.
2 Run python manage.py syncdb. Since you have added a new application to INSTALLED_APPS, the database tables need to be updated.
3 Edit your mysite/urls.py file and uncomment the lines that reference the admin – there are three lines in total to uncomment. This file is a URLconf; we’ll dig into URLconfs in the next tutorial. For now, all you need to know is that it maps URL roots to applications. In the end, you should have a urls.py file that looks like this:
bear@njava:~/njava$ vi settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'njava.news'
}
bear@njava:~/njava$python manage.py syncdb
bear@njava:~/njava$ vi urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^tt/', include('tt.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
bear@njava:~/njava$python manage.py runserver 0.0.0.0:8000
现在就可以用localhost:8000 登录了
二 把app关联到admin界面
bear@njava:~/njava/news$ vi admin.py from tt.news.models import Post from django.contrib import admin admin.site.register(Post) bear@njava:~/njava/news$cd .. bear@njava:~/njava$python manage.py runserver 0.0.0.0:8000
三 自定义模板
bear@njava:~/njava$vi settings.py
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"/home/njava/Sites/django/templates"
)
copy django/contrib/admin/templates/admin/base_site.html to /home/njava/Sites/django/templatess/admin/base_site.html. Don’t forget that admin subdirectory.
link:http://docs.djangoproject.com/en/1.2/intro/tutorial02/#intro-tutorial02