django basis (a) - to create a project, modify the configuration

--- --- restore content begins

Premise: installed django, django-admin input on the command line, check if you have installed

First, create a Django project

method one:

1. build a folder, such as "demo"

Command line into this folder, execute the command: django-admin startproject dj_test. This command creates a django project in this folder. Create a "dj_test" directory after execution. Name their own definition

dj_test in Py file:

setting.py: Profile

urls.py: Configure url

3. command line: cd dj_test enter django project (dj_test) to create, run python manage.py runserver: start the service:

4. After the service starts, access 127.0.0.1:8000. Normally open page is created.

5.ctrl + c out of service, the command line: Python manage.py startapp user to create a subsystem. We must create an app after you create a project, user custom name. After you create a subsystem, the project will generate a file db.sqlite

Method Two: Use pycharm created:

1. Open pycharm, FILE - close project in welcome to pycharm interface, point create new project, choose Django project, and then select the location directory (day18), Python environment

2.more settings in the Application name is to create the name of APP (subsystem), designated here as "user", do not write it will not create an app.

 

two:

Structure explanation:

1.manage.py: Project Manager, such as modifying the database, you need to use manage.py to operate

2.templates: put html file

3. project file documents:

(1) settings.py: Profile

(2) urls.py: Configure the url

(3) wsgi.py: After the completion of the development, deployment files to the server when used

4. subsystem (user) files created:

(1) models.py: Operation of the database table structure defined

(2) views.py: Write the main logic

5.migrations: stored table structure

 

Third, modify the configuration file:

1.setting.py:

(1) LANGUAGE_CODE = 'en-us' to LANGUAGE_CODE = 'zh-Hans'. The change here is the language, the English changed to Chinese. Code (line 108)

(2) TIME_ZONE = 'UTC' to TIME_ZONE = 'Asia / Shanghai'. Here is to modify the time zone, the standard time zone changed to Shanghai.

(3) USE_TZ = True to USE_TZ = False. Change is here to save time after the database is correct, the default is True, the default is stored into the database of the time standard time zone.

After the modification, run the project (day18) or enter python manage.py runserver in Terminal, the access 127.0.0.1:8000 page into Chinese

(4) Line 78

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

The default database is the sqlite, sqlite database file, you can also write sql statement. sqlite is a local database.

If you need to change mysql database, the dictionary to replace the default code may be used by the database mysql

    ' Default ' : {
         ' ENGINE ' : ' django.db.backends.mysql ' , # database engine to MySQL
         ' NAME ' : ' JXZ ' , # database name
         ' the USER ' : ' JXZ ' , user #
         ' PASSWORD ' : ' 123456 ' , # password
         ' HOST ' : ' 118.24.3.40 ' , # ip
         'PORT': 3306 , port number # 
    }

The project will run into mysql error, the solution:

In the project directory (day18) init.py file and add the code. This is to be used Pymysql as SQLdb

import pymysql
pymysql.install_as_MySQLdb()

If you are running the wrong version of the problem of the Times, reduces django version:

View django version: command line Python execution environment

import django
django.VERSION

If django version is higher than 2.1, to perform pip install django == 2.1, install the 2.1 version of django.

Here we do not have mysql database, using the default django sqlite. So first into the mysql default commented out, and replaced with the original default. Exercise with default data, then changed mysql deployment.

(5) line 55, where TEMPLATES responsible for configuring the urls file path, if the project is created by the command, this list does not " 'DIRS': [os.path.join (BASE_DIR, 'templates')]" This a row of data, with the creation of the project pycharm is this line

= TEMPLATES [ 
    { 
        ' BACKEND ' : ' django.template.backends.django.DjangoTemplates ' ,
         ' the DIRS ' : [the os.path.join (base_dir, ' Templates ' )] # If the command item is created, this list no " 'DIRS': [os.path.join ( bASE_DIR, 'templates')]" this row of data, with the creation of the project pycharm is this line 
        , 
        ' APP_DIRS ' : True,
         ' the OPTIONS ' : {
             ' context_processors ' : [
                 ' django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

 

 

 

 

 

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/hancece/p/11695051.html