关于cmd:html
返回上一层: cd..python
切换到D盘: d:正则表达式
进入目录: cd:C:\Python27\Scripts\HelloWorddjango
安装好Django后第一个程序:浏览器
一、cmd进入安装django目录服务器
二、输入:python manage.py runserver 0.0.0.0:8000app
三、浏览器 输入:127.0.0.1:8000函数
Next, start your first app by running python manage.py startapp [app_label]
.this
You're seeing this message because you have DEBUG = True
in your Django settings file and you haven't configured any URLs. Get to work! url
出现上面的字样,表示成功。
创建一个view.py
from django.http import HttpResponse def hello(request): return HttpResponse("Hello world ! ")
修改urls.py文件
from django.conf.urls import url from . import view urlpatterns = [ url(r'^$', view.hello), ]
完成后,启动 Django 开发服务器,并在浏览器访问打开浏览器并访问:http://127.0.0.1:8000
Django url() 能够接收四个参数,分别是两个必选参数:regex、view 和两个可选参数:kwargs、name,接下来详细介绍这四个参数。
regex: 正则表达式,与之匹配的 URL 会执行对应的第二个参数 view。
view: 用于执行与正则表达式匹配的 URL 请求。
kwargs: 视图使用的字典类型的参数。
name: 用来反向获取 URL。
url(r'^&')^&中间就是网址的尾缀,view.hello :就是view里面的方法例如
url(r'^hello&view.view.hello)
网址为:http:/127.0.0.1:8000/hello引用view里面的hello方法
使用
hello.html 文件代码以下:
<h1>{{ hello }}</h1>
修改:settings.py 特别注意:修改头应该加上# -*- coding: utf-8 -*-,缘由:里面有 # 修改位置,这是中文哦!!!
...TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR+"/templates",], # 修改位置 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] ...
修改view.py
# -*- coding: utf-8 -*- #from django.http import HttpResponse from django.shortcuts import render def hello(request): context = {} context['hello'] = 'Hello World!' return render(request, 'hello.html', context)
访问:http://127.0.0.1:8000