赞
踩
1. Ubuntu Django安装:
pip install django
2. django-admin.py创建一个项目
django-admin.py 是django的管理工具,使用 django-admin.py 可以方便的创建项目,创建一个名称为HelloWorld的项目:
django-admin.py startproject HelloWorld
在当前目录下会创建一个名称为“HelloWorld”的文件夹,查看这个文件夹的目录结构:
- cd HelloWorld
- tree
显示如下:
- .
- ├── HelloWorld
- │ ├── __init__.py
- │ ├── settings.py
- │ ├── urls.py
- │ └── wsgi.py
- └── manage.py
3. 运行web服务
python manage.py runserver
显示:
- Django version 1.11.11, using settings 'HelloWorld.settings'
- Starting development server at http://127.0.0.1:8000/
- Quit the server with CONTROL-C.
运行成功,127.0.0.1是ip,8000是端口号。在浏览器中输入 127.0.0.1:8000 进行访问:
- It worked!
- Congratulations on your first Django-powered page.
-
- Next, start your first app by running python manage.py startapp [app_label].
-
- 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!
或者:
python manage.py runserver 0.0.0.0:8050
需要把ip‘0.0.0.0’加入到 settings.py 的ALLOWED_HOSTS中:
ALLOWED_HOSTS = ['0.0.0.0']
可以在浏览器里通过 0.0.0.0:8050 或127.0.0.1:8050 访问,默认是8000端口。
在HelloWorld目录下创建 view.py 文件,代码:
- from django.http import HttpResponse
- def hello(request):
- return HttpResponse("Hello world ! ")
绑定URL与视图函数:
修改HelloWorld目录下 urls.py 文件内容为:
- from django.conf.urls import url
- from . import view
- urlpatterns = [
- url(r'^$', view.hello),
- ]
之后启动Django开发服务器,在浏览器中访问 127.0.0.1:8000 ,显示内容:
Hello world !
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。