django 简易版搭建

时间:2024-12-16 17:06:57
1、根目录下创建mysql.cnf文件
[client]
database = identimg
user = root
password = root
host = 127.0.0.1
port = 3306
default-character-set = utf8 2、修改settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR,"mysql.cnf"),
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
} 3、更新pip
//  卸载
pip uninstall pip
// 重新安装
easy_install pip pip -V
// 安装mysql驱动,
pip install mysqlclient 最近装了一次,竟然一直不成功。 555 ~ pip install mysqlclient==1.3.10 这样却是成功的 // 生成数据库表
python manage.py migrate // 创建 polls应用
python manage.py startapp polls
// settings.py 中 添加应用
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

// polls下创建urls.py
from django.urls import path
from .SpeakM import test urlpatterns = [
path('speak',test)
]
// 修改项目目录下的urls.py 为
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/',include('apiService.urls'))
]
// 创建模型
from django.db import models
// polls/models.py 中
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published') class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0) // 创建数据迁移
python manage.py makemigrations polls // 数据迁移对应的 sql
python manage.py sqlmigrate polls 0001 // 再次数据迁移
python manage.py migrate

总结数据库操作流程

创建管理员账号

python manage.py createsuperuser

// 在 polls/admin.py 中,将Question加入管理

from django.contrib import admin

from .models import Question

admin.site.register(Question)

创建测试

//  将下面的代码写入 polls 应用里的 tests.py 文件内:

import datetime

from django.test import TestCase
from django.utils import timezone from .models import Question class QuestionModelTests(TestCase):

# 测试方法必须test 开头
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)

运行测试

python manage.py test polls

## 结果
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_question
self.assertIs(future_question.was_published_recently(), False)
AssertionError: True is not False ----------------------------------------------------------------------
Ran 1 test in 0.001s FAILED (failures=1)
Destroying test database for alias 'default'...