Django 项目实践01

August 24, 2022

这篇文章是关于Django项目实践的教程。首先,我们介绍了如何安装Django。然后,我们创建了一个新的Django项目,并进行了数据库的初始化。接着,我们启动了Django服务,并展示了如何在浏览器中查看项目。最后,我们创建了一个新的应用程序,并定义了一个简单的模型。这篇文章是对Django项目实践的基础介绍,适合初学者阅读。

安装django

sudo pip install django

创建项目

(python3.9.6)  django-admin startproject learning_log .
(python3.9.6)  ll
total 8
drwxr-xr-x  7 iarno  staff   224B  8 24 23:15 learning_log
-rwxrwxrwx  1 iarno  staff   668B  8 24 23:15 manage.py

创建数据库

(python3.9.6)  python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

启动服务

(python3.9.6) ➜ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 24, 2022 - 15:17:29
Django version 3.2.7, using settings 'learning_log.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

image-20220824232233025

创建应用程序

(python3.9.6)  python manage.py startapp learning_logs
(python3.9.6)  ll
(python3.9.6)  learning_log ls
db.sqlite3    learning_log  learning_logs ll_env        manage.py
(python3.9.6)  learning_log ls learning_logs 
__init__.py __pycache__ admin.py    apps.py     migrations  models.py   tests.py    views.py

定义模型

# learning_logs/models.py
from django.db import models

# Create your models here.

class Topic(models.Model):
    """用户学习主题"""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.text
PythonPython编程从入门到实战Django

IARNO

服务端开发

gin当请求对象为数组时,binding不生效问题解决

Python 单元测试