简介
Django 最初被设计用于具有快速开发需求的新闻类站点,目的是要实现简单快捷的网站开发。
设计模型
Django 无需数据库就可以使用,它提供了 对象关系映射器 通过此技术,你可以使用 Python 代码来描述数据库结构。
你可以使用强大的 数据-模型语句 来描述你的数据模型,这解决了数年以来在数据库模式中的难题。以下是一个简明的例子:
from django.db import models
class Reporter(models.Model):
full_name = models.CharField(max_length=70)
def __str__(self):
return self.full_name
class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
def __str__(self):
return self.headline
Django安装
使用pip安装
pip3 install django==3.2
Django基本使用
创建一个Django项目
django-admin startproject huhuhahei
启动项目
python3 manage.py runserver
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 14, 2023 - 05:52:11
Django version 3.2.16, using settings 'huhuhahei.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C
如果需要监听0.0.0.0 可以在启动的时候指定监听地址 0.0.0.0:8080
网页访问打开这样就证明启动成功了
配置后台页面
访问后台页面
访问报错找不到表 这里我们需要先初始化下数据库
#初始化数据库
python3 manage.py makemigrations
No changes detected
#创建库表
python3 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
#创建admin用户
python3 manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: admin
Error: Enter a valid email address.
Email address: admin@^C
Operation cancelled.
[root@blog huhuhahei]# python3 manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: admin@qq.com
Password:
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
再次访问
创建一个应用
python3 manage.py startapp app01
查看文件结构
tree
.
├── app01
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── huhuhahei
│ ├── asgi.py
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
setting文件注册应用
INSTALLED_APPS = [
.......
'app01',
]
创建一个视图的函数
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("hello world")
配置url路由修改urls文件
#添加
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('app01/', views.index),
]
访问 http://106.14.172.136:8080/app01/
配置模块
models文件添加
from django.db import models
# Create your models here.
from django.db import models
PROJECT_TYPE = [
(0,"app"),
(1,"web"),
(2,"其他")
]
class Project(models.Model):
project_name = models.CharField(max_length=200, blank=False,verbose_name="项目名称")
project_type = models.SmallIntegerField(blank=False,choices=PROJECT_TYPE,verbose_name="项目类型")
project_version = models.CharField(max_length=200, blank=False,verbose_name="项目版本")
admin模块添加
from django.contrib import admin
# Register your models here.
from app01.models import Project
admin.site.register(Project)
#初始化数据库
python3 manage.py makemigrations
#创建库表
python3 manage.py migrate
访问后台页面
http://106.14.172.136:8080/admin/
点击去可以正常添加我们需要的文本
Django优化
全局优化
admin 文件添加
admin.site.site_header = "平台管理"
admin.site.site_title = "管理后台"
列表优化
class ProjectAdmin(admin.ModelAdmin):
list_display = ("project_name","project_type","project_version")
admin.site.register(Project, ProjectAdmin)
导航栏优化
class HuhuhaheiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app01'
verbose_name = "测试平台"