目前,我的环境配置已经好了,版本python==3.7 and django==2.2
安装django:
pip install django==2.2 -i https://pypi.douban.com/simple/
开始创建项目:
#创建项目: django-admin startproject project01 #创建APP python manage.py startapp hello #运行 python manage.py runserver 0.0.0.0:8080
如果浏览器不能打开,2个原因:firewall 和 setting
要把防火墙关掉 and 修改setting.py 文件
查看防火墙状态: firewall-cmd --state 停止firewall systemctl stop firewalld.service 启动firewall systemctl start firewalld.service 禁止firewall开机启动 systemctl disable firewalld.service
修改setting文件
ALLOWED_HOSTS = ['*']
将项目的语言改为中文等等
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
接下来呢,创建一个视图
#项目urls from django.contrib import admin from django.urls import path from hello import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.index, name="index"), ] # app views from django.http import HttpResponse # Create your views here. def index(request): return HttpResponse("<h1>hello world</h1>")
浏览器打开结果: