python3 django finishing (six) configuration database (mysql)

mysql driver under python3

The default driver for django to connect to mysql is MySQLdb. MySQLdb does not have a version that supports python3. If python3.x version is used, the method for django to connect to mysql
1. Use pymysql to replace MySQLdb and add the following code to the init.py
file in the same directory of the configuration file

 import pymysql
pymysql.install_as_MySQLdb()

write picture description here
2. Use mysqlclient instead of MySQLdb. The address of the mysqlclient project on github is https://github.com/PyMySQL/mysqlclient-python . This project fork MySQLdb and added support for python3. The
installation method is:

pip install mysqlclient

Use the same way as MySQLdb

Configure settings

Step 2: Configure the mysql connection parameters in settings.py (install mysql without mysql)
and rewrite the original database configuration as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends. mysql',
'NAME': 'Database name (you have to create a database in mysql first)',
'USER': 'mysql username (eg root)',
'PASSWORD': 'password (eg 123456789)',
'HOST ':'Domain name (127.0.0.1 or localhost)',
'PORT':'Port number (3306)',
}
}
write picture description here

Create and manipulate data

Create the model class in models.py

Step 3: Create the model class in models.py

from django.db import models

# Create your models here. 类似于MVC架构中的Model
class Article(models.Model):
    title = models.CharField(max_length=60,default='title')
    content = models.TextField(null=True)

Step 4: Create a database table based on the model class

1. cmd enters the django project path
2. python manage.py migrate #Create table structure, other tables that are not model classes, required by django
3. python manage.py makemigrations app name # Prepare for data migration
such as : python manage .py makemigrations myblog myblog is the app name in my project
4. python manage.py migrate # Execute migration and create medel table structure

Step 5: Start writing code

First of all, let’s talk about the requirements, that is, insert a record into mysql in the code and display it on the page
1. Create a new template under templates, which is actually a page, such as index.html
{{article.title}}
Content:{{ article.content }}
Use {{ }} to display data on the page, you can see it here
2. Configure URL
1. Configure url mapping in urls.py under the project (note the urls.py under the project):

from django.conf.urls import url,include
from django.contrib import admin
#根url配置
urlpatterns = [
    #url(页面正则,响应的方法名称)
    url(r'^admin/', admin.site.urls),
    url(r'^myblog/',include('myblog.urls')),
]

Note here that there is an include ('myblog.urls') which is the secondary url we will configure next, which is configured in urls.py under the app

from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
    #url(页面正则,响应的方法名称) ^index$:表示要以index开始和结束,正则约束
    url(r'^index/$',views.index),
]

Now an access path with a path of 'localhost:8000/myblog/index/' is ready, url(r'^index/$', views.index) means that the final path /myblog/index/ is created by views.py The index method in the response.
3. Write a response function: such as inserting a data into the data and displaying it on the page

from django.shortcuts import render
from django.http import HttpResponse
from myblog.models import Article
# Create your views here.
def index(request):
    article = Article(title='标题',content='内容!')
    article.save()
    return render(request,'index.html',{'article':article}

Step 6: Run the project

The pycharm I use here, just click the run button, there is no pycharm available:

python manage.py runserver

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324385262&siteId=291194637