Django+Xadmin construction project

Part Django

  1. Create project

    django-admin startproject mysite #Create a mysite project
    
  2. Run easy server

    python manage.py runserver  #Welcome page http://127.0.0.1:8000
    
  3. Set system language and time

    //File name: settings.py
    *********************
    LANGUAGE_CODE = 'zh-hans'
    
    TIME_ZONE = 'Asia/Shanghai'
    
  4. Set up MySQL database

    Create a database before changing the settings file

    ********Under project directory__init__.py Add the following code to the file********
    import pymysql
    pymysql.install_as_MySQLdb()
    *******************END****************************
    
    ***************settings.py file*********************
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': 'localhost',
            'PORT': '3306',
            'NAME': 'dadi',
            'USER': 'root',
            'PASSWORD': 'root',
        }
    }
    ****************END********************************
    
  5. Database migration

    python manage.py migrate #Create database table according to setting.py file
    
  6. Create administrator account

    python manage.py createsuperuser
    

    So far, a Django project has been built, and the browser opens http://127.0.0.1:8000/admin You can use the set administrator account to log in to the management background. APP has not been created yet. To create APP, run py in the project directory manage.py startapp app_name

Introducing xadmin

  1. Install xadmin

    pip install git+git://github.com/sshwsfc/xadmin.git@django2
    
  2. Set urls.py file

    import xadmin //Add this trip
    from django.conf import settings //Add this trip
    from django.conf.urls.static import static //Add this trip
    from django.urls import path
    
    urlpatterns = [
        path('admin/', xadmin.site.urls), //Revision of this trip
    ]
    
  3. settings.py file

    import xadmin  //Add this trip
    
    INSTALLED_APPS = [
        'xadmin', //Add this trip
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'crispy_forms'  //Add this trip
    ]
    
  4. Execute database migration command again

    python manage.py migrate
    

    After the migration is completed, you can log in with the background management account you created before. At this time, you can see that the background interface has changed.

Tags: Django Database Python git

Posted on Sat, 30 Nov 2019 02:38:20 -0800 by fingerprn