搭建django项目【极精简】
一、django简介
Python语言编写的的Web应用框架
二、django项目搭建
利用pycharm搭建django项目,如图。
点击【create】,即创建成功。点击右上角【绿色三角】,运行django项目。
运行结果如下。
底部小黑框,可以看到项目默认网址:http://127.0.0.1:8000/。
底部小黑框,出现以下问题“Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions” 。
点击【terminal】,输入:python manage.py migrate,点击【回车】运行。
问题解决!
在网页输入网址:http://127.0.0.1:8000/。
出现:“The install worked successfully! Congratulations!”恭喜,项目运行成功。
三、运行“hello world!”
在app1/views.py文件,
输入:
from django.http import HttpResponse
def index(request):
return HttpResponse("hello world!hello python!")
pass
在django项目/urls.py文件,
导入模块:
from app1 import views as app1_views
在“urlpatterns”中加入【新path】,同时注释(#)【原始path】,如图:
path("",app1_views.index),
#path('admin/', admin.site.urls),
在网页输入网址:http://127.0.0.1:8000/。
出现:“hello world!hello python!”恭喜,项目运行成功。
三、运行【.html】文件
在django项目/templates文件,新建【html1.html】文件。
在djano项目内,新建文件【static】,用来存放css、js和图片。
在【html1.html】文件输入自己想展现的效果。
打开app1/views.py文件,修改index函数,如图:
def index(request):
#return HttpResponse("hello world!hello python!")
return render(request, "html1.html")
pass
在网页输入网址:http://127.0.0.1:8000/。
出现:“这是一个django项目!”恭喜,项目运行成功。