zoukankan      html  css  js  c++  java
  • Python3集成安装xadmin

    Python3集成安装xadmin
    1:创建虚拟环境
    C:UsersAdminstrator>mkvirtualenv -p C:Python34python.exe MyDjango
    如果提示找不到,可以直接mkvirtualenv MyDjango
    创建成功以后则终端会这样显示:
    (MyDjango)C:UsersAdminstrator
    2:安装依赖,如果不安装依赖的话在使用xadmin的时候会各种报错。依赖列表如下:

    django
    Pillow
    django-crispy-forms~=1.6.0
    django-import-export>=0.5.1
    django-reversion~=2.0.0
    django-formtools
    future==0.15.2
    httplib2==0.9.2
    six==1.10.0
    django-pure-pagination==0.3.0
    django-simple-captcha
    mysqlclient
    requests
    xlrd
    XlsxWriter
    xlwt

    使用pip install -i https://pypi.douban.com/simple django Pillow ..进行安装
    使用豆瓣的源会比国外的下载更快
    在安装mysqlclient可能会出现下面的错误(主要是windows环境下会有此问题):
    error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows S
    DK 7.1": www.microsoft.com/download/details.aspx?id=8279

    ----------------------------------------
    Failed building wheel for mysqlclient

    解决办法:去这个路径下下载mysqlclient:
    https://www.lfd.uci.edu/~gohlke/pythonlibs/
    根据每个人操作系统的情况下载对应的版本,然后放到安装路径下并进行安装。例如:
    (MyDjango) C:UsersCCD>pip install mysqlclient-1.3.12-cp34-cp34m-win32.whl
    Processing c:usersccdmysqlclient-1.3.12-cp34-cp34m-win32.whl
    Installing collected packages: mysqlclient
    Successfully installed mysqlclient-1.3.12
    安装完成以后,就可以在cmd下使用pip install查看是否已经安装完成。
    3:新建一个Django工程,工程名取为DjangoLearn

    我们通过源码的方式安装xadmin,下载xadmin源码,然后在工程中新建一个文件夹extra_apps
    直接把xadmin源码文件夹拖到extra_apps中,因为新建的工程默认使用admin,所以我们需要进行一些修改
    在工程中的settings.py文件夹中的INSTALLED_APPS增加 xadmin和crispy_forms,crispy-forms可以用来
    渲染我们的表单。
    然后修改我们的url,需要右击extra_apps将这个路径设置为Source Root:Mark Directory as Source Root

    import xadmin
    urlpatterns = [
    path('xadmin/', xadmin.site.urls),
    ]

    如果Django工程没有配置数据库的话,就需要进行数据库的配置,数据库配置如下:

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': "testdjango",
    'USER': "root",
    'PASSWORD': "123456",
    'HOST': "127.0.0.1",
    }
    }

    然后点击工程中的Tools一栏
    选择Run manage.py Task
    输入命令makemigrations和migrate进行数据库创建:
    如果出现这样的错误:

    Failed to get real commands on module "DjangoLearn": python process died with code 1: Traceback (most recent call last):
    File "D:Program FilesJetBrainsPyCharm 2017.2.3helperspycharm\_jb_manage_tasks_provider.py", line 25, in <module>
    django.setup()
    File "C:UsersCCDEnvsMyDjangolibsite-packagesdjango\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
    File "C:UsersCCDEnvsMyDjangolibsite-packagesdjangoapps
    egistry.py", line 85, in populate
    app_config = AppConfig.create(entry)
    File "C:UsersCCDEnvsMyDjangolibsite-packagesdjangoappsconfig.py", line 90, in create
    module = import_module(entry)
    File "C:UsersCCDEnvsMyDjangolibimportlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
    File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
    File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
    ImportError: No module named 'xadmin'

    则需要在setting中增加这样以下代码:
    import sys
    sys.path.insert(0,os.path.join(BASE_DIR,'extra_apps')) 这样就可以把extra_apps路径放到当前模块扫描的路径中

    想登陆xadmin,需要先在Pycharm中创建用户

    createsuperuser
    manage.py@DjangoLearn > createsuperuser
    "D:Program FilesJetBrainsPyCharm 2017.2.3in
    unnerw.exe" C:UsersCCDEnvsMyDjangoScriptspython.exe "D:Program FilesJetBrainsPyCharm 2017.2.3helperspycharmdjango_manage.py" createsuperuser F:/PycharmProjects/DjangoLearn
    Username (leave blank to use 'ccd'): CCD
    Email address: 123456789@qq.com
    Warning: Password input may be echoed.
    Password: ABC123456
    Warning: Password input may be echoed.
    Password (again): ABC123456
    Superuser created successfully.
    Following files were affected
    Process finished with exit code 0

    然后访问http://127.0.0.1:8000/xadmin/

    就可以登录上后台管理系统了

  • 相关阅读:
    csrf 跨站请求伪造
    apache 根据端口访问配置
    对于表单中单双引号问题
    svn常用命令
    urlencode和rawurlencode的区别
    php 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前
    javaweb基础(24)_jsp一般的标签开发
    javaweb基础(23)_jsp自定义标签
    javaweb基础(22)_Servlet+JSP+JavaBean实战登陆
    javaweb基础(21)_两种开发模式
  • 原文地址:https://www.cnblogs.com/BGPYC/p/8011728.html
Copyright © 2011-2022 走看看