django學習之xadmin後臺管理部署

首先看下admin的後臺管理界面(默認,未做任何設置)

wKioL1R-hzfBRFW_AAFeC6-wTqs701.jpg

wKiom1R-hsyxJ8p-AAIlDobRqDc811.jpg


都說xadmin很吊,吊炸天,我就拿過來擼了一把,發現事實並不是這樣的,我只能說一句,最合適自己的纔是最好的,還是自己擼吧,偶爾借鑑下還是可以的~ 不要太沉迷於這些框架~


開工~


安裝須知:

  • django >=1.4

  • django-crispy-forms >=1.2.3 (For xadmin crispy forms)

  • django-reversion ([OPTION] For object history and reversion feature, please select right version by your django, see changelog )

  • xlwt ([OPTION] For export xls files)

  • xlsxwriter ([OPTION] For export xlsx files)


個人建議:

  • 建議使用django==1.5的版本,高或者低都有bug,親測

  • 有bug,請提交,爲xadmin貢獻一下,當然能自我修復bug並提交,最好不過了

  • 感謝作者~

  • 本篇文章只是簡單的安裝,沒有涉及到真正的應用,so~ 



步驟:

  1. 創建虛擬環境

[root@php ~]# pythonbrew venv create dj
Creating `dj` environment into /root/.pythonbrew/venvs/Python-2.7.6
Already using interpreter /root/.pythonbrew/pythons/Python-2.7.6/bin/python
New python executable in /root/.pythonbrew/venvs/Python-2.7.6/dj/bin/python
Installing setuptools............done.
Installing pip...............done.
[root@php ~]# pythonbrew venv use dj
# Using `dj` environment (found in /root/.pythonbrew/venvs/Python-2.7.6)
# To leave an environment, simply run `deactivate`
(dj)[root@php ~]# ls


2、安裝django==1.5

(dj)[root@php ~]# pip install 'django==1.5'
Downloading/unpacking django==1.5
  Downloading Django-1.5.tar.gz (8.0MB): 8.0MB downloaded
  Running setup.py egg_info for package django
    
    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755
    
    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
    changing mode of /root/.pythonbrew/venvs/Python-2.7.6/dj/bin/django-admin.py to 755
Successfully installed django
Cleaning up...
(dj)[root@php ~]#


3、創建一個名爲blog的項目

(dj)[root@php ~]# django-admin.py startproject blog
(dj)[root@php ~]# cd blog/
(dj)[root@php blog]# ls
blog  manage.py
(dj)[root@php blog]# tree .
.
├── blog
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
1 directory, 5 files


4、創建一個名爲polls的app

(dj)[root@php blog]# python manage.py startapp polls
(dj)[root@php blog]# ls
blog  manage.py  polls
(dj)[root@php blog]# ll
total 12
drwxr-xr-x 2 root root 4096 Dec  2 03:29 blog
-rw-r--r-- 1 root root  247 Dec  2 03:28 manage.py
drwxr-xr-x 2 root root 4096 Dec  2 03:29 polls


安裝xadmin

(dj)[root@php blog]# pip install django-xadmin
Downloading/unpacking django-xadmin
  Downloading django-xadmin-0.5.0.tar.gz (1.0MB): 1.0MB downloaded
  Running setup.py egg_info for package django-xadmin
    
Requirement already satisfied (use --upgrade to upgrade): setuptools in /root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg (from django-xadmin)
Requirement already satisfied (use --upgrade to upgrade): django>=1.5 in /root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/site-packages (from django-xadmin)
Downloading/unpacking django-crispy-forms>=1.4.0 (from django-xadmin)
  Downloading django-crispy-forms-1.4.0.tar.gz (47kB): 47kB downloaded
  Running setup.py egg_info for package django-crispy-forms
    
    warning: no files found matching '*' under directory 'crispy_forms/static'
Installing collected packages: django-xadmin, django-crispy-forms
  Running setup.py install for django-xadmin
    
  Running setup.py install for django-crispy-forms
    
    warning: no files found matching '*' under directory 'crispy_forms/static'
Successfully installed django-xadmin django-crispy-forms
Cleaning up...
(dj)[root@php blog]#


調整相關參數讓xadmin可以正常訪問

  1. 在settings文件中添加剛纔我們創建的polls這個app

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.           # 設置數據庫使用sqlite3
        'NAME': 'db.sqlite3',                      # Or path to database file if using sqlite3.                                         # 數據庫名稱爲db.sqlite3
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'polls',  # 添加該行
    ‘xadmin', # 添加該行
)


2、設置urls文件

from django.conf.urls import patterns, include, url
import xadmin                 # 添加該行
xadmin.autodiscover()     # 添加該行
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from xadmin.plugins import xversion    # 添加該行
xversion.registe_models()                    # 添加該行
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.foo.urls')),
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
    url(r'^xadmin/', include(xadmin.site.urls)),            # 添加該行
)


3、收集media信息

(dj)[root@php blog]# python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
0 static files copied.
(dj)[root@php blog]#


4、同步數據庫

(dj)[root@php blog]# python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'root'): zhuima
Email address: 
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
(dj)[root@php blog]#


5、開啓服務,前臺登陸測試

(dj)[root@php blog]# python manage.py runserver 0.0.0.0:80
Validating models...
0 errors found
December 01, 2014 - 20:59:40
Django version 1.5, using settings 'blog.settings'
Development server is running at http://0.0.0.0:80/
Quit the server with CONTROL-C.


6、出現報錯

AttributeError at /xadmin/
'module' object has no attribute 'atomic'
Request Method:GET
Request URL:http://192.168.58.21/xadmin/
Django Version:1.5
Exception Type:AttributeError
Exception Value:
'module' object has no attribute 'atomic'
Exception Location:/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/site-packages/reversion/admin.py in VersionAdmin, line 384
Python Executable:/root/.pythonbrew/venvs/Python-2.7.6/dj/bin/python
Python Version:2.7.6
Python Path:
['/root/blog',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
 '/root/.pythonbrew/pythons/Python-2.7.6/lib',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python27.zip',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/plat-linux2',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/lib-tk',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/lib-old',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/lib-dynload',
 '/root/.pythonbrew/pythons/Python-2.7.6/lib/python2.7',
 '/root/.pythonbrew/pythons/Python-2.7.6/lib/python2.7/plat-linux2',
 '/root/.pythonbrew/pythons/Python-2.7.6/lib/python2.7/lib-tk',
 '/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/site-packages']
Server time:Mon, 1 Dec 2014 21:29:24 -0600


6、解決辦法(個人解決方法,不建議採用)

註釋掉`/root/.pythonbrew/venvs/Python-2.7.6/dj/lib/python2.7/site-packages/reversion/admin.py in VersionAdmin, line 384` 文件中包含`atomic`字樣的信息,然後重啓服務即可


7、前端顯示

wKioL1R-ikjBUQAoAAGHqUNQBq0740.jpgwKiom1R-ibzyfebgAAFmclWG8-I589.jpg

spacer.gif

再次感謝xadmin的作者~ 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章