Python Web框架Django入门

安装启动

Python:https://www.python.org/downloads/
Django:https://www.djangoproject.com/download/

1
2
3
4
> pip install Django==3.0.7  #pip方式安装
> django-admin startproject HelloWorld #创建项目
> cd HelloWorld
> python manage.py runserver 0.0.0.0:8000 #启动服务

数据库及用户

创建默认库

1
python manage.py migrate

创建默认超级用户

1
python manage.py createsuperuser

视图和 URL 配置

新建文件HelloWorld/HelloWorld/views.py

1
2
3
4
from django.http import HttpResponse

def hello(request):
return HttpResponse("Hello world!")

绑定 URL 与视图函数。编辑文件HelloWorld/HelloWorld/urls.py

1
2
3
4
5
6
7
8
9
from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', views.hello),
path('', views.hello),
]

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :