Django+mysql Configuration and Simple Operation Database Instance Code

  • 2021-08-28 21:23:32
  • OfStack

Step 1: Download the mysql driver

cmd Enter the created django project directory: Use the command


pip install mysqlclient

Wait for the installation to succeed!

Step 2: Configure mysql connection parameters in settings. py (preloaded mysql without mysql)


DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': ' Database name ( You have to first mysql Create a database in )',
    'USER':'mysql User name, such as root ) ',
    'PASSWORD':' Password (e.g. 123456789 ) ',
    'HOST':' Domain name ( 127.0.0.1 Or localhost ) ',
    'PORT':' Port number ( 3306 ) ',
  }
}

Step 3: Create the model class in models. py


from django.db import models
# Create your models here.  Similar to MVC In the schema Model
class Article(models.Model):
  title = models.CharField(max_length=60,default='title')
  content = models.TextField(null=True)

Step 4: Create a database table from the model class

1. cmd enters the django project path

2. Python manage. py migrate # Create table structure, other tables not of model class, required by django

3. python manage. py makemigrations app # Prepare for data migration

For example: python manage. py makemigrations myblog myblog is the name of app in my project

4. python manage. py migrate # Performs migration and creates medel table structure

Step 5: Start writing code

First of all, the requirement is to 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


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<h2> {{article.title}}</h2>
 Content :{{ article.content }}
</body>
</html>

Use {{}} to display data on the page, and you can see it here

2. Configure URL

1. Configure the url mapping in urls. py under the project (note urls. py under the project):


from django.conf.urls import url,include
from django.contrib import admin
# Root url Configure 
urlpatterns = [
  #url( Page regularity , Method name of response )
  url(r'^admin/', admin.site.urls),
  url(r'^myblog/',include('myblog.urls')),
]

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


from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
  #url( Page regularity , Method name of response ) ^index$: Indicates that you want to use index Beginning and ending, regular constraints 
  url(r'^index/$',views.index),
]

An access path with the path 'localhost: 8000/myblog/index/' is now matched, and url (r '^ index/$', views. index) means that eventually/myblog/index/this path is responded to by the index method in views. py.

3. Write response function: For example, insert 1 data into data and display 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=' Title ',content=' Content! ')
  article.save()
  return render(request,'index.html',{'article':article}

Step 6: Run the project

The pycharm I use here can be used by clicking the Run button. There is no pycharm available:


python manage.py runserver

To start the server, then enter http://localhost: 8000/myblog/index/in the browser, and call it a day!


Related articles: