Django + mysql configuration and simple operation of database instance code, djangomysql
Step 1: Download the mysql driver
Run cmd to enter the created django project directory: Use the command
pip install mysqlclient
Wait until the installation is successful!
Step 2: Configure mysql connection parameters in settings. py (mysql is installed first without mysql)
DATABASES = {'default': {'Engine ': 'django. db. backends. mysql ', 'name': 'database NAME (You must create a database in mysql first)', 'user': 'mysql username (such as root) ', 'Password ': 'password (such as 123456789) ', 'host': 'domain name (127.0.0.1 or localhost)', 'Port': 'port number (3306 )',}}
Step 3: create a model class in models. py
From django. db import models # Create your models here. similar to Modelclass 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. Run cmd to enter the django project path.
2. Python manage. py migrate # create a table structure, which is required by django instead of other tables in the model class
3. python manage. py makemigrations app name # Prepare for data migration
For example, python manage. py makemigrations myblog is the name of the app in my project.
4. python manage. py migrate # execute migration and create the medel table structure
Step 5: Start writing code
First, you need to insert a record to MySQL in the Code and display it on the page.
1. Create a new template under templatesand its real-name page, such as index.html
<! DOCTYPE html>
Use {} to display data on the page. You can see it here.
2. Configure the URL
1. Configure url ing in the urls. py of the project (Note: urls. py of the project:
From django. conf. urls import url, includefrom django. contrib import admin # configure urlpatterns in the root url = [# url (page regular, response method name) url (R' ^ admin/', admin. site. urls), url (R' ^ myblog/', include ('myblog. urls '),]
Note that there isinclude('myblog.urls')Is the second-level url to be configured next, which is configured in urls. py under the app
From django. conf. urls import urlfrom django. contrib import adminfrom. import viewsurlpatterns = [# url (page regular expression, response method name) ^ index $: indicates to start and end with index. Regular Expressions constrain url (R' ^ index/$ ', views. index),]
Now an access path with the path 'localhost: 8000/myblog/index/'is configured, url (R' ^ index/$', views. index) indicates that the final/myblog/index/path is composed of views. the index method in py to respond.
3. Write a response function. For example, insert a data file into the data file and display it on the page.
From django. shortcuts import renderfrom django. http import HttpResponsefrom myblog. models import Article # Create your views here. def index (request): article = Article (title = 'title', content = 'content! ') Article. save () return render(request,'index.html', {'Article': article}
Step 6: run the project
Click the run button for the pycharm I use here. It is usable without pycharm:
python manage.py runserver
Open the server, and then enter http: // localhost: 8000/myblog/index/in the browser!
The above is the Django + mysql configuration and simple operation of the database instance Code introduced by the editor. I hope it will help you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!