django 之HELLO WORLD
1. 安装PYTHON:
apt-get install python root@redis1:~# python --version Python 2.7.12
2. 安装PYTHON 包管理软件PIP:
apt-get install python-pip
3. 安装VIRTUALENV:
apt-get install virtualenv
4. 虚拟一个环境:
virtualenv python root@redis1:~# . python/bin/activate (python) root@redis1:~#
5. 安装DJANGO:
(python) root@redis1:~# pip install django >>> import django >>> django.VERSION (1, 11, 10, u'final', 0)
6. 创建一个项目:
(python) root@redis1:~# django-admin.py startproject study (python) root@redis1:~# ls -l study/ total 8 -rwxr-xr-x 1 root root 803 Feb 9 06:25 manage.py drwxr-xr-x 2 root root 4096 Feb 9 06:25 study
7. 创建一个APP:
(python) root@redis1:~# cd study/ (python) root@redis1:~/study# python manage.py startapp myapp (python) root@redis1:~/study# ls manage.py myapp study (python) root@redis1:~/study# ls -l myapp/ total 24 -rw-r--r-- 1 root root 0 Feb 9 06:28 __init__.py -rw-r--r-- 1 root root 128 Feb 9 06:28 admin.py -rw-r--r-- 1 root root 150 Feb 9 06:28 apps.py drwxr-xr-x 2 root root 4096 Feb 9 06:28 migrations -rw-r--r-- 1 root root 122 Feb 9 06:28 models.py -rw-r--r-- 1 root root 125 Feb 9 06:28 tests.py -rw-r--r-- 1 root root 128 Feb 9 06:28 views.py
8. 修改项目study下的SETTINGS配置文件:
8.1 增加自己的APP:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', ]
8.2 修改时区及语言:
LANGUAGE_CODE = 'zh-cn' TIME_ZONE = 'Asia/shanghai' ALLOWED_HOSTS = [*]
9. 增加一个URL列表:
(python) root@redis1:~/study# vi study/urls.py from django.conf.urls import url from django.contrib import admin from myapp import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^myapp/index/$',views.index), ]
10. 修改myapp下的views.py文件,增加视图函数:
(python) root@redis1:~/study# cat myapp/views.py # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render from django.http import HttpResponse # Create your views here. def index(request): return HttpResponse("<h1>Hello World!!!!!</h1>")
11. 启动服务,并尝试访问:
(python) root@redis1:~/study# python manage.py runserver 0.0.0.0:8080 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. February 09, 2018 - 07:01:27 Django version 1.11.10, using settings 'study.settings' Starting development server at http://0.0.0.0:8080/ Quit the server with CONTROL-C.
[dbadmin@i-rqn56rhe ~]$ curl http://192.168.252.100:8080/myapp/index/ <h1>Hello World!!!!!</h1>
访问原理图: