django创建模型失败
介绍 (Introduction)
In the previous tutorial, “How To Create a Django App and Connect it to a Database,” we covered how to create a MySQL database, how to create and start a Django application, and how to connect it to a MySQL database.
在上一个教程“ 如何创建Django应用程序并将其连接到数据库 ”中,我们介绍了如何创建MySQL数据库,如何创建和启动Django应用程序以及如何将其连接到MySQL数据库。
In this tutorial, we will create the Django models that define the fields and behaviors of the Blog application data that we will be storing. These models map the data from your Django application to the database. It’s what Django uses to generate the database tables via their object relational mapping (ORM) API, referred to as “models.”
在本教程中,我们将创建Django 模型 ,这些模型定义将要存储的Blog应用程序数据的字段和行为。 这些模型将数据从Django应用程序映射到数据库。 Django通过其对象关系映射(ORM)API(称为“模型”)来生成数据库表。
先决条件 (Prerequisites)
This tutorial is part of the Django Development series and is a continuation of that series.
本教程是Django开发系列的一部分,并且是该系列的继续。
If you have not followed along with this series, we are making the following assumptions:
如果您未遵循本系列文章,我们将进行以下假设:
- You have Django version 3 or higher installed. 您已安装Django版本3或更高版本。
You have connected your Django app to a database. We are using MySQL, and you can achieve this connection by following part two of the Django series, “How To Create a Django App and Connect it to a Database.”
您已将Django应用程序连接到数据库。 我们正在使用MySQL,您可以通过遵循Django系列的第二部分“ 如何创建Django应用并将其连接到数据库 ”来实现此连接。
You are working with a Unix-based operating system, preferably an Ubuntu 20.04 cloud server as this is the system we have tested on. If you would like to set up Django on a similar environment, please refer to our tutorial, “How To Install Django and Set Up a Development Environment on Ubuntu 20.04.”
您正在使用基于Unix的操作系统,最好是Ubuntu 20.04云服务器,因为这是我们经过测试的系统。 如果您想在类似的环境中设置Django,请参考我们的教程“ 如何在Ubuntu 20.04上安装Django和设置开发环境 ”。
As this tutorial is largely dealing with Django models, you may be able to follow along even if you have a somewhat different setup.
由于本教程主要涉及Django模型,因此即使设置有所不同,您也可以继续学习。
第1步-创建Django应用程序 (Step 1 — Create Django Application)
To be consistent with the Django philosophy of modularity, we will create a Django app within our project that contains all of the files necessary for creating the blog website.
为了与Django模块化原则保持一致,我们将在我们的项目中创建一个Django应用,其中包含创建博客网站所需的所有文件。
Whenever we begin doing work in Python and Django, we should activate our Python virtual environment and move into our app’s root directory. If you followed along with the series, you can achieve this by typing the following.
每当我们开始在Python和Django中进行工作时,都应激活Python虚拟环境并移至应用程序的根目录。 如果按照该系列进行学习,则可以通过键入以下内容来实现。
- cd ~/my_blog_app cd〜/ my_blog_app
- . env/bin/activate 。 env / bin / activate
- cd blog cd博客
From there, let’s run this command:
从那里开始,运行以下命令:
- python manage.py startapp blogsite python manage.py startapp博客网站
This will create our app along with a blogsite
directory.
这将创建我们的应用程序以及blogsite
目录。
At this point in the tutorial series, you’ll have the following directory structure for your project:
在本系列教程的这一点上,您将具有以下项目的目录结构:
my_blog_app/
└── blog
├── blog
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── blogsite
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── manage.py
The file we will focus on for this tutorial, will be the models.py
file, which is in the blogsite
directory.
在本教程中,我们将重点关注的文件是在blogsite
目录中的models.py
文件。
第2步-添加帖子模型 (Step 2 — Add the Posts Model)
First we need to open and edit the models.py
file so that it contains the code for generating a Post
model. A Post
model contains the following database fields:
首先,我们需要打开和编辑models.py
文件,使其包含用于生成Post
模型的代码。 Post
模型包含以下数据库字段:
title
— The title of the blog post.title
—博客文章的标题。slug
— Where valid URLs are stored and generated for web pages.slug