django创建模型失败_如何创建Django模型

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:

如果您未遵循本系列文章,我们将进行以下假设:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值