Django 框架

Django 是用 Python 开发的一个免费开源的 Web 框架,几乎囊括了 Web 应用的方方面面,可以用于快速搭建高性能、优雅的网站,Django 提供了许多网站后台开发经常用到的模块,使开发者能够专注于业务部分。

Django 提供了通用 Web 开发模式的高度抽象,为频繁进行的编程作业提供了快速解决方法,并为“如何解决问题”提供了清晰明了的约定。Django 通过 DRY(Don’t Repeat Yourself,不要重复自己)的理念来鼓励快速开发。

测试环境

  • python:3.14.4
  • HGDB:9.0.5.1
  • 操作系统:windows 11

安装及配置

安装psysopg2

PS C:\Users\highgo> python -m pip install psycopg2
Collecting psycopg2
Downloading psycopg2-2.9.12-cp314-cp314-win_amd64.whl.metadata (5.1 kB)
Downloading psycopg2-2.9.12-cp314-cp314-win_amd64.whl (2.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.8/2.8 MB 11.8 MB/s 0:00:00
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.12

安装Django

PS C:\Users\guoluyu> python -m pip install django
Collecting django
Downloading django-6.0.5-py3-none-any.whl.metadata (3.9 kB)
Collecting asgiref>=3.9.1 (from django)
Downloading asgiref-3.11.1-py3-none-any.whl.metadata (9.3 kB)
Collecting sqlparse>=0.5.0 (from django)
Downloading sqlparse-0.5.5-py3-none-any.whl.metadata (4.7 kB)
Requirement already satisfied: tzdata in .\AppData\Local\Programs\Python\Python314\Lib\site-packages (from django) (2025.3)
Downloading django-6.0.5-py3-none-any.whl (8.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.4/8.4 MB 10.7 MB/s 0:00:00
Downloading asgiref-3.11.1-py3-none-any.whl (24 kB)
Downloading sqlparse-0.5.5-py3-none-any.whl (46 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.11.1 django-6.0.5 sqlparse-0.5.5

配置示例

找到项目中的settings.py文件,修改DATABASES部分的配置:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5866',
}
}

测试用例

1. 新建项目目录并创建Django项目

PS D:\> mkdir django_hg_test

Directory: D:\

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2026/6/3 16:04 django_hg_test

PS D:\> cd .\django_hg_test\
PS D:\django_hg_test> django-admin startproject myproject .
PS D:\django_hg_test> ls

Directory: D:\django_hg_test

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2026/6/3 16:05 myproject
-a--- 2026/6/3 16:05 687 manage.py

2. 修改配置文件

修改myproject/settings.py文件,添加数据库配置:

# Database
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'djangotest',
'USER': 'highgo',
'PASSWORD': 'qwe123',
'HOST': '192.168.245.142',
'PORT': '5866',
}
}

3. 创建应用并定义模型

PS D:\django_hg_test> python manage.py startapp users

将'users'添加到myproject/settings.py文件的INSTALLED_APPS中:

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
]

编辑users/models.py:

from django.db import models

class Person(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(unique=True)

def __str__(self):
return self.name

4. 执行迁移并测试

PS D:\django_hg_test> python manage.py makemigrations
Migrations for 'users':
users\migrations\0001_initial.py
+ Create model Person
PS D:\django_hg_test> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, users
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
Applying users.0001_initial... OK

如果没有错误,说明 Django 已成功连接到 HGDB 并创建了数据表:

djangotest=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------------------------------+----------+--------
public | auth_group | table | highgo
public | auth_group_id_seq | sequence | highgo
public | auth_group_permissions | table | highgo
public | auth_group_permissions_id_seq | sequence | highgo
public | auth_permission | table | highgo
public | auth_permission_id_seq | sequence | highgo
public | auth_user | table | highgo
public | auth_user_groups | table | highgo
public | auth_user_groups_id_seq | sequence | highgo
public | auth_user_id_seq | sequence | highgo
public | auth_user_user_permissions | table | highgo
public | auth_user_user_permissions_id_seq | sequence | highgo
public | django_admin_log | table | highgo
public | django_admin_log_id_seq | sequence | highgo
public | django_content_type | table | highgo
public | django_content_type_id_seq | sequence | highgo
public | django_migrations | table | highgo
public | django_migrations_id_seq | sequence | highgo
public | django_session | table | highgo
public | users_person | table | highgo
public | users_person_id_seq | sequence | highgo
(21 rows)

测试写入数据:

PS D:\django_hg_test> python manage.py shell
13 objects imported automatically (use -v 2 for details).

Python 3.14.4 (tags/v3.14.4:23116f9, Apr 7 2026, 14:10:54) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from users.models import Person
>>> Person.objects.create(name='张三', email='zhang@example.com')
<Person: 张三>
>>> exit()
now exiting InteractiveConsole...

可以看到数据已经写入数据库:

djangotest=# select * from users_person;
id | name | email
----+------+-------------------
1 | 张三 | zhang@example.com
(1 row)

至此,Django 已成功连接到 HGDB 并可进行其他操作。