Django常用配置

创建Django项目(命令行)

创建项目:打开终端,使用命令:django-admin startproject [项目名称]即可创建。比如:django-admin startproject first_project

创建应用(app):python manage.py startapp [app名称]

项目文件配置

settings.py中修改如下代码段

创建数据库

# 进入数据库
C:\Users\leo>mysql -uroot -p
# 输入密码
Enter password: **********
# 创建数据库并设定字符集
mysql> create database GPAXF charset=utf8;
Query OK, 1 row affected (0.01 sec)

# 创建可以让Django读取的数据
mysql> grant all privileges on GPAXF.* to leo@'%' identified by '1q2w3E!';
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)

截图如下所示:
Django常用配置

配置数据库

DATABASES = {
    'default': {
        # 数据库引擎(是mysql还是oracle等)
        'ENGINE': 'django.db.backends.mysql',
        # 数据库的名字
        'NAME': 'GPAXF',
        # 连接mysql数据库的用户名
        'USER': 'leo',
        # 连接mysql数据库的密码
        'PASSWORD': '1q2w3E!',
        # mysql数据库的主机地址
        'HOST': '127.0.0.1',
        # mysql数据库的端口号
        'PORT': '3306',
    }
}

pycharm连接数据库

Django常用配置

汉化网址

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

配置静态资源

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
# 媒体文件
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uploads')

配置数据库驱动

init.py中修改

import pymysql
pymysql.install_as_MySQLdb()

配置路由

# 项目文件中的urls.py
import debug_toolbar
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('axf/', include(('App.urls', 'axf'), namespace='axf')),
    path('__debug__/', include(debug_toolbar.urls)),
]
# app文件夹中的urls.py
from App import views
from django.urls import path, re_path

urlpatterns = [
    # path('', views.home, name="home"),
    path('home/', views.home, name="home"),
    re_path(r'marketwithparams/(?P<typeid>\d+)/(?P<childcid>\d+)/(?P<order_rule>\d+)/', views.market_with_params,
            name="market_with_params"),
    # path('marketwithparams/<typeid>/', views.market_with_params, name="market_with_params"),
    path('market/', views.market, name="market"),
    path('cart/', views.cart, name="cart"),
    path('mine/', views.mine, name="mine"),
    path('register/', views.register, name='register'),
    path('login/', views.login, name='login'),
    path('logout/', views.logout, name='logout'),
    path('activate/', views.activate, name='activate'),
    path('addtocart/', views.add_to_cart, name='add_to_cart'),
    path('changecartstate/', views.change_cart_state, name='change_cart_state'),
    path('subshopping/', views.sub_shopping, name='sub_shopping'),
    path('addShopping/', views.addShopping, name='addShopping'),
    path('allselect/', views.allselect, name="allselect"),
    path('makeorder/', views.makeorder, name='makeorder'),
    path('orderdetail/', views.order_detail, name='order_detail'),
    path('orderlistnotpay/', views.order_list_not_pay, name='order_list_not_pay'),
    path('payed/', views.payed, name='payed'),
]
上一篇:开始第一个Django demo例子


下一篇:Django2.x - 模板过滤器