Creating a Django project (command line)

Create a project: Open a terminal and run the command Django-admin startProject [project name] to create a project. For example: Django-admin startproject first_project.

Create app: Python manage.py startapp [app name]

Project File Configuration

Modify the following code segment in settings.py

Creating a database

# Enter databaseC:\Users\leo>mysql -uroot -p# Enter passwordEnter password: ********** # Create database and set character setmysql> create database GPAXF charset=utf8;
Query OK1,row affected (0.01 sec) # Create can letDjangoRead datamysql> grant all privileges on GPAXF. *to leo@ '%'identified by '1q2w3E! ';Query OK. 0rows affected1,warning (0.04 sec)

mysql> flush privileges;
Query OK. 0rows affected (0.04 sec)
Copy the code

The screenshot is as follows:

Configuring the database

DATABASES = {
    'default': {
        Database engine (mysql, Oracle, etc)
        'ENGINE': 'django.db.backends.mysql'.# Name of the database
        'NAME': 'GPAXF'.User name for connecting to mysql database
        'USER': 'leo'.# Password to connect to mysql database
        'PASSWORD': '1q2w3E! '.# host address of mysql database
        'HOST': '127.0.0.1'.# Port number of the mysql database
        'PORT': '3306',}}Copy the code

Pycharm connects to the database

Localization url

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False
Copy the code

Configuring Static Resources

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),]# Media files
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uploads')
Copy the code

Configuring the Database Driver

Modify in init.py

import pymysql
pymysql.install_as_MySQLdb()
Copy the code

Configure the routing

# in the project file 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)),
]
Copy the code
# app folder in 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
      
       \d+)/(? P
       
        \d+)/(? P
        
         \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'),]Copy the code