美文网首页Django
django+amdin+xadmin 后台管理系统 crash

django+amdin+xadmin 后台管理系统 crash

作者: ynds | 来源:发表于2020-03-21 01:41 被阅读0次

    应用场景

    方便爬虫数据做可视化展示,xamdin相比于admin界面更友好,功能强大,增加了数据导出功能

    requirements

    python3.7
    django在应大于2.1小于3.0, xadmin 安装django2版本
    pip install django==2.1.5
    pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2

    根据数据源所在数据库,安装相应包
    pip install pymongo
    pip install mysqlclient

    step by step

    创建虚拟环境
    mkvirtualenv django_demo
    选择虚拟环境
    workon django_demo
    进入项目准备存放的文件夹
    cd <项目准备存放的文件夹>
    创建datashow项目
    django-admin startproject datashow
    进入项目
    cd datashow
    创建app
    python manage.py startapp meituan
    配置setting.py

    # 部署到公网
    ALLOWED_HOSTS = ['*']
    
    # 添加app
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        #app
        'meituan',
        #xadmin
        'xadmin',
        'crispy_forms',
        'reversion',
    ]
    
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        # 语言国际化,配置之后下边LANGUAGE_CODE会失效,无法完成汉化
        # 'django.middleware.locale.LocaleMiddleware', 
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    
    ]
    
    # 配置语言
    LANGUAGE_CODE = 'zh-Hans'
    
    TIME_ZONE = 'Asia/Shanghai'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = True
    

    配置urls.py

    import xadmin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'xadmin/', xadmin.site.urls),
    ]
    

    进入meituan app 编写models.py

    from django.db import models
    
    
    class Waimai(models.Model):
        id = models.AutoField('序号', primary_key=True)
        shop_name = models.CharField('店名', max_length=20)
        city = models.CharField('城市', max_length=10)
        district = models.CharField('区县', max_length=10)
        sales_nums = models.IntegerField('销量')
        telephone = models.CharField('联系方式', max_length=50)
    
        def __str__(self):
            return self.shop_name
    
        class Meta:
            verbose_name = '美团外卖'
            verbose_name_plural = verbose_name
    
    

    创建并迁移数据库

    #打开控制台,回到根目录
    python manage.py makemigrations
    python manage.py migrate
    

    编写脚本将mongo数据库数据导入项目sqlite数据库

    # 回到项目根目录(mange.py同级目录)
    #创建 load2db.py
    
    import pymongo
    import os
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "datashow.settings")
    import django
    django.setup()
    from meituan.models import Waimai
    
    
    # 将数据导入数据库
    def load_data():
       uri = "mongodb://localhost:27017/"
       results = pymongo.MongoClient(uri)['meituan_waimai']['每城600含经纬度'].find()
       L = []
       for result in results:
           wm = Waimai()
           wm.shop_name = result.get('shop_name','')
           wm.city = result.get('city','')
           wm.district = result.get('district','')
           try:
               wm.sales_nums = int(result.get('sale_nums'))
           except:
               wm.sales_nums = 0
           wm.telephone = result.get('telephone','')
           # wm.save()
           print(wm)
           L.append(wm)
       # 单条插入效率比批量插入慢很多
       Waimai.objects.bulk_create(L)
    
    
    if __name__ == "__main__":
       load_data()
       print('Done!')
    

    运行脚本导入数据
    进入meituan app,新建amdin.py同级文件adminx.py

    import xadmin
    
    from .models import *
    
    
    class WaimaiAdmin(object):
    
        list_display = ['id','shop_name','city','district','sales_nums','telephone']
        list_filter = ['city','district','sales_nums']
        search_fields = ['shop_name','city','district','telephone']
        # ordering = ['id'] # 默认为每个字段设置排序
    
    xadmin.site.register(Waimai,WaimaiAdmin)
    

    控制台创建超级用户

    python manage.py createsuperuser
    # 接下来填写用户名密码和邮箱,邮箱用于找回密码
    

    运行项目

    python mange.py runserver
    

    相关文章

      网友评论

        本文标题:django+amdin+xadmin 后台管理系统 crash

        本文链接:https://www.haomeiwen.com/subject/hwseyhtx.html