基本介绍
Django 是一个由 Python 编写的一个开放源代码的 Web 应用框架。
使用 Django,只要很少的代码,Python 的程序开发人员就可以轻松地完成一个正式网站所需要的大部分内容,并进一步开发出全功能的 Web 服务 Django 本身基于 MVC 模型,即 Model(模型)+ View(视图)+ Controller(控制器)设计模式,MVC 模式使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能。
pip安装
pip3 install django -i https://pypi.douban.com/simple
创建项目
django-admin startproject 项目名称
启动项目
python manage.py runserver
终端显示
E:\qhost\Qhost>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
April 14, 2022 - 03:37:54
Django version 4.0.4, using settings 'Qhost.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
浏览器输入:http://127.0.0.1:8000/
创建app
python manage.py startapp user
- app目录说明
注册app
- 打开项目的目录找到配置文件settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'APP名称.apps.UserConfig',//在这里注册app ]
- 编写URL视图函数【app名称/url.py】
from django.urls import path
from user import views
urlpatterns = [
# path('admin/', admin.site.urls),
path('index/',views.index)
]
- 在app名称/url.py里创建index函数
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
return HttpResponse('hello word')
- 启动项目访问http://127.0.0.1:8000/index/
没有回复内容