摘要:本文主要向大家介绍了Python语言 django,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言 django,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
1、安装django
2、查django 版本 python -m django --version
3、创建django 项目
cd C:\Curtis\Python\Project
4、django-admin startproject MySite
MySite 是创建的django 项目名称
5、cd MySite
6、创建app
python manage.py startapp helloapp
helloapp 是创建的应用的名称
7、python manage.py runserver
8、在浏览器访问输入://127.0.0.1:8000
============================================================================================================================
第一个程序:hello world
1、views.py 文件
2、引入HttpResponse 对象
3、定义函数
3.1、函数名任意取;函数参数request;函数返回值 return HttpResponse(request,'hello world')
4、在项目路由映射文件中配置
代码
views.py 文件
from django.http import HttpResponse
def hello(request):
return HttpResponse("hello world! I'm coming...")
urls.py 文件
from helloapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.hello),
]
============================================================================================================================
应用下创建路由映射文件,在项目映射文件中引入;
MTV
models、templates、views 文件
应用下创建templates 文件夹,用于放静态资源
urlpatterns 单词固定的
代码:
urls.py 文件
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello)
]
项目路由映射文件中引用
代码:
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('index2/', include('hello2app.urls')),
]
============================================================================================================================
templates 文件夹下面的*.html 文件使用
代码:
应用项目views.py
from django.shortcuts import render
def hello(request):
return render(request, '1.html')
项目配置文件中设置DIRS
'DIRS': [os.path.join(BASE_DIR, 'hello2app/templates')],
以上就介绍了Python的相关知识,希望对Python有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号