环境准备:
linux+python+djangolinux系统一般都已经安装了python环境
可以通过python –version查看是否安装
[root@VM_153_111_centos HelloWord]# python --version
Python 2.7.5
- django安装
Mc:Tools root# pip install Django==1.11
Collecting Django==1.11
Downloading Django-1.11-py2.py3-none-any.whl (6.9MB)
100% |████████████████████████████████| 6.9MB 105kB/s
Requirement already satisfied: pytz in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from Django==1.11)
Installing collected packages: Django
Successfully installed Django-1.11
#检查django是否安装成功
Mc:djangospace SmartWang$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.11'
4.创建django一个叫HelloWord的django项目
django-admin.py startproject HelloWorld
Mc:HelloWord SmartWang$ tree -a
.
├── HelloWord
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
1 directory, 5 files
目录说明:
HelloWorld: 项目的容器。
manage.py: 一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互。
HelloWorld/__init__.py: 一个空文件,告诉 Python 该目录是一个 Python 包。
HelloWorld/settings.py: 该 Django 项目的设置/配置。
HelloWorld/urls.py: 该 Django 项目的 URL 声明; 一份由 Django 驱动的网站"目录"。
HelloWorld/wsgi.py: 一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。
5.启动django服务
进入 HelloWorld 目录输入:python manage.py runserver 0.0.0.0:8888,启动服务器,默认端口号为8000,只要是空闲端口均可:
Mc:HelloWord SmartWang$ python manage.py runserver 0.0.0.0:8888
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 22, 2017 - 08:14:31
Django version 1.11, using settings 'HelloWord.settings'
Starting development server at http://0.0.0.0:8888/
Quit the server with CONTROL-C.
Invalid HTTP_HOST header: '0.0.0.0:8888'. You may need to add u'0.0.0.0' to ALLOWED_HOSTS.
[22/Apr/2017 08:14:56] "GET / HTTP/1.1" 400 60511
Invalid HTTP_HOST header: '0.0.0.0:8888'. You may need to add
[22/Apr/2017 08:16:28] "GET /favicon.ico HTTP/1.1" 404 1965
...
在浏览器中输入网址:http://127.0.0.1:8888/,出现以下截图证明成功了
6.视图和 URL 配置
在先前创建的 HelloWorld 目录下的 HelloWorld 目录新建一个 view.py 文件,并输入代码:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello Word")
接着,绑定 URL 与视图函数。打开 urls.py 文件,删除原来代码,将以下代码复制粘贴到 urls.py 文件中:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.conf.urls import url
from . import view
urlpatterns = [
url(r'^$', view.hello),
]
完成后,启动 Django 开发服务器,并在浏览器访问打开浏览器并访问:http://127.0.0.1:8888/
修改urls.py里的代码如下:
from django.conf.urls import url
from . import view
urlpatterns = [
url(r'^hello$', view.hello),
]
启动浏览器:http://127.0.0.1:8888/hello,查看结果
7.在服务器搭建在客户端端访问
django-admin.py startproject project-name创建的项目中去修改 setting.py 文件:
ALLOWED_HOSTS = [‘*’] #在这里请求的host添加了*,于是就成功的访问到了Django的项目了;
参考:1.视图和URL配置
http://blog.csdn.net/houht2013/article/details/11369193
2.Django 创建第一个项目
www.runoob.com/django/django-first-app.html
3.DisallowedHost at / Invalid HTTP_HOST header
http://blog.csdn.net/will5451/article/details/53861092