ORM查询条件-准备工作
使用cmd命令在该项目下创建一个包
python manage.py startapp front
在settngs.py中配置这个包 以及 配置数据库
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'front' ]
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'demo3', 'USER': 'root', 'PASSWORD': '******', 'HOST': '127.0.0.1', 'PORT': '3306', } }
在models.py创建两个模型
from django.db import models class Article(models.Model): title = models.CharField(max_length=200) content = models.TextField() class Meta: db_table = 'article' #更改这个表的名字,映射到数据中的话名称不是Article_article了,是你自定义设置的article
使用下面这两条命令在数据库中映射:(注意进入项目路径以及有虚拟环境的进入虚拟环境)
python manage.py makemigrations
python manage.py migrate
接着在 views.py中做一下基本的映射:
from django.shortcuts import render from django.http import HttpResponse from .models import Article def index(request): return HttpResponse('success')
然后在urls.py中做映射这个视图函数即可
from django.urls import path from front import views urlpatterns = [ path('',views.index), ]
顺便打开navicat在这张表中插入两条数据,方便后面的操作

这样基本的准备工作就完成了

浙公网安备 33010602011771号