Install mysql, create database or import database
Second step: Win10 build django2.1.7 development environment, create project for mytestSite, create app for Quicktool
Step 3: Edit the configuration file (mytestsite/settings.py) of the folder with the same name as the project to configure the database information
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE':'django.db.backends.mysql'.'NAME':'Database name'.'USER':'Database account'.'PASSWORD':'Database password'.'HOST':'Database address, localhost if it's local.'.'PORT':'Database port, default 3306',}}Copy the code
Step 4: Edit the __init__.py file (mytestsite/init.py) of the folder with the same name as the project to import the mysql database
import pymysql
pymysql.install_as_MySQLdb()
Copy the code
Step 5: Use the command in the manage.py path of the project
python manage.py inspectdb > D:\django_test\mytestsite\quicktool\models.py
Copy the code
Integrate the existing database and application, generate models, and copy the models to the models.py command line of the application app (quicktool/models.py). If no error is returned at the end of the command line, success is achieved. The class name is the data table name, inherited from models.model, and each class defines the field name within the data table
Error: the django. Core. Exceptions. ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; You have 0.7.11. None
Find Python37\Lib\site-packages\ Django \db\backends\mysql\base.py in the Python installation directory and comment the following code
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
Copy the code
Error: File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\mysql\operations.py” , line 146, in last_executed_query query = query.decode(errors=’replace’) AttributeError: ‘str’ object has no attribute ‘decode’
Find the file code line, decode can be changed to encode
Step 6: Query the table using Django shell and return the results
Run the python manage.py shell command in the manage.py path of the project
AssertionError: Model QuickTool. AppVersionInfo can’t have more than one AutoField.
python manage.py shell
from quicktool.models import AppVersionInfo
AppVersionInfo.objects.get(version_code=10)
Copy the code
The database has the table AppVersionInfo and the field version_code
CREATE TABLE `app_version_info` (version_code` int(3) NOT NULL DEFAULT '0')Copy the code
The query results print the ID value, and do not display specific related data table information
def __str__(self):
# def __unicode__(self) in Python3:
return self.url
Copy the code
After the new method is added, it prints the specific data of the data table
Step 7: Visualize the results of the query into the quicktool/views.py file on the web page:
from django.http import HttpResponse
from quicktool.models import AppVersionInfo
def index(request):
appl = AppVersionInfo.objects.get(version_code=25)
return HttpResponse(str(appl))
Copy the code
Mytestsite mytestsite/urls. Py file path, the rest of the omitted:
from quicktool import views as quicktool_views # new
urlpatterns = [
path(' ', quicktool_views.index, name='home'), # new
]
Copy the code
Quicktool/templates/home. HTML files of the body, the rest of the omitted:
<body>
{{ appl }}
</body>
Copy the code
The query results are displayed on the web page as follows: