首页 > 自动化运维 > python > Django 安装Xadmin增强后台管理
2015
02-10

Django 安装Xadmin增强后台管理

前言:

? ? ? ? ? 之前就打算做一个自动化的运维平台,但是一直没有时间研究django,这几天工作不是很忙,不知不觉就开始折腾django了,在看到xadmin这个后台的时候,我真的特别兴奋,因为它的外观太完美了。但是在环境安装的时候遇到了一些问题,导致我的项目搁置不前。在此我整理了一份安装笔记,希望大家少走弯路,不要把时间浪费在环境的配置上,更多的时间去开发自己的平台。

环境介绍:

Centos6.5 (IP:192.168.1.155) ?python 2.6 ? ? ?django 1.5

一、安装django环境

yum?install?python-devel?mysql-devel?zlib-devel?openssl-devel??mysql?mysql-server?zlib?zlib-devel

二、创建一个工程并查看目录结构

[root@localhost?Django-1.5.12]#?cd?/usr/local/
?[root@localhost?local]#?django-admin.py?startproject?mysite
?[root@localhost?local]#?tree?mysite/
?mysite/
?├──?manage.py
?└──?mysite
?├──?__init__.py
?├──?settings.py
?├──?urls.py
?└──?wsgi.py
1?directory,?5?files
?[root@localhost?local]#

三、启动django访问测试

[root@localhost?local]#?cd?mysite/
?[root@localhost?mysite]#?python?manage.py?runserver?0.0.0.0:8000
?Validating?models...
0?errors?found
?January?31,?2015?-?13:55:28
?Django?version?1.5.12,?using?settings?'mysite.settings'
?Development?server?is?running?at?http://0.0.0.0:8000/
?Quit?the?server?with?CONTROL-C.


ok到此,我们的工程创建成功

四、安装xadmin使用pip直接安装

[root@localhost?zdong]#?pip?install?django-reversion
[root@localhost?zdong]#?pip?install?MySQL-python
[root@localhost?zdong]#?pip?install?django-xadmin
?Collecting?django-xadmin
?Downloading?django-xadmin-0.5.0.tar.gz?(1.0MB)
?100%?|################################|?1.0MB?224kB/s
?/tmp/pip-build-FlsY1Q/django-xadmin/setup.py:12:?DeprecationWarning:

1、安装成功xadmin成功之后,启用xadmin

?[root@localhost?zdong]#?cd?/usr/local/mysite/mysite/
?[root@localhost?mysite]#?ls
?__init__.py?__init__.pyc?settings.py?settings.pyc?urls.py?urls.pyc?wsgi.py?wsgi.pyc
在settings.py?中修改如下信息
?#配置你的数据库连接信息
DATABASES?=?{
?'default':?{
?'ENGINE':?'django.db.backends.mysql',?#?Add?'postgresql_psycopg2',?'mysql',?'sqlite3'?or?'oracle'.
?'NAME':?'xadmin',?#?Or?path?to?database?file?if?using?sqlite3.
?#?The?following?settings?are?not?used?with?sqlite3:
?'USER':?'zdong',
?'PASSWORD':?'123456',
?'HOST':?'127.0.0.1',?#?Empty?for?localhost?through?domain?sockets?or?'127.0.0.1'?for?localhost?through?TCP.
?'PORT':?'',?#?Set?to?empty?string?for?default.
?}
?}
#APPS?中添加xadmin的信息
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',
?############################
?'xadmin',
?'crispy_forms',
?'reversion',
?############################
urls.py配置如下
from?django.conf.urls?import?patterns,?include,?url
?import?xadmin
?xadmin.autodiscover()
?from?xadmin.plugins?import?xversion
?xversion.register_models()
?urlpatterns?=?patterns('',
?url(r'^xadmin/',?include(xadmin.site.urls),?name='xadmin'),
?)

2、数据库的配置以及授权

[root@localhost?mysite]#?mysql
?Welcome?to?the?MySQL?monitor.?Commands?end?with?;?or?\g.
?mysql>?create?database?xadmin;
?Query?OK,?1?row?affected?(0.00?sec)
?mysql>?grant?all?on?xadmin.*?to?'zdong'@'127.0.0.1'?identified?by?'123456';
?Query?OK,?0?rows?affected?(0.01?sec)
?mysql>?flush?privileges;
?Query?OK,?0?rows?affected?(0.00?sec)
?mysql>?quit
?Bye

3、收集media文件

[root@localhost?mysite]#?python?manage.py?collectstatic
?AttributeError:?'module'?object?has?no?attribute?'atomic'
?[root@localhost?mysite]#?看到这个问题,我纠结了好久,一直寻找不到解决的办法,后来就直接改xadmin的代码了,注释了atomic的信息。

解决办法(不建议使用,希望能找到更好的办法)

vim?/usr/lib/python2.6/site-packages/reversion/admin.py?文件中?atomic的信息
?384??????#?@transaction.atomic
?385?#?def?recover_view(self,?request,?version_id,?extra_context=None):
?386?#?"""Displays?a?form?that?can?recover?a?deleted?model."""
?387?#?#?check?if?user?has?change?or?add?permissions?for?model
?388?#?if?not?self.has_change_permission(request)?and?not?self.has_add_permission(request):
?389?#?raise?PermissionDenied
?390?#?version?=?get_object_or_404(Version,?pk=version_id)
?391?#?obj?=?version.object_version.object
?392?#?context?=?{"title":?_("Recover?%(name)s")?%?{"name":?version.object_repr},}
?393?#?context.update(extra_context?or?{})
?394?#?return?self.render_revision_form(request,?obj,?version,?context,?recover=True)
?395
?396?#?@transaction.atomic

在次收集media的信息

[root@localhost?mysite]#?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

ok成功了

4、同步数据到数据库并启动django

root@localhost?mysite]#?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
?Creating?table?reversion_revision
?Creating?table?reversion_version
?Creating?table?xadmin_bookmark
?Creating?table?xadmin_usersettings
?Creating?table?xadmin_userwidget
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'):?admin????#用户名是登陆后台的用户名
?Email?address:?123456@qq.com
?Password:??????????????????????????????????#登陆后台的密码
?Password?(again):
?Superuser?created?successfully.
?Installing?custom?SQL?...
?Installing?indexes?...
?Installed?0?object(s)?from?0?fixture(s)
?[root@localhost?mysite]#
[root@localhost?mysite]#?python?manage.py?runserver?0.0.0.0:8000

五、使用游览器登陆来看看效果吧

1.png

2.png

 

OK到此结束,希望这篇文章能帮你节省安装所花费的时间。

 




最后编辑:
作者:tshare365
这个作者貌似有点懒,什么都没有留下。
捐 赠您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请狠狠点击

留下一个回复