安装Django:
wget https://www.djangoproject.com/m/releases/1.7/Django-1.7.2.tar.gztar -zxf Django-1.7.2.tar.gzcd Django-1.7.2/python setup.py install**** error:Traceback (most recent call last): File "setup.py", line 4, infrom setuptools import setup, find_packagesImportError: No module named setuptools****
缺少setuptools模块,需要安装该模块:
下载最新setuptools:https://pypi.python.org/packages/source/s/setuptools/
tar -zxf setuptools-9.1.tar.gz cd setuptools-9.1/python setup.py installcd ../Django-1.7.2/python setup.py install
测试安装是否成功:
pythonPython 2.7.3 (default, Mar 18 2014, 05:13:23) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import django #无报错则成功。
# django-admin startproject dj01 #创建一个工程dj01# tree dj01/ #默认工程目录树dj01/├── dj01│ ├── __init__.py│ ├── settings.py #框架配置文件│ ├── urls.py #url配置文件│ └── wsgi.py└── manage.py1 directory, 5 files# django-admin startapp blog# tree blogblog├── admin.py├── __init__.py├── migrations│ └── __init__.py├── models.py #模块配置文件├── tests.py└── views.py #视图配置文件
----修改配置文件:
settings.pyINSTALLED_APPS = ('blog', #添加该行)
# vim urls.pyfrom django.conf.urls import patterns, include, urlfrom django.contrib import adminurlpatterns = patterns('', url(r'^blog/index$', 'blog.views.index'),#当用户访问blog/index的时候,指定访问到blog目录下的views模块的index方法 url(r'^blog/abc$', 'blog.views.abc'),#同上)
# vim views.py #blog目录下的views.py文件,定义具体的访问内容,视图页面from django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.def index(req): return HttpResponse('Hello welcome to Django! ')def abc(req): return HttpResponse('dddddddddddd')
---ok,下面测试
# python manage.py runserver #启动服务,测试!Performing system checks...System check identified no issues (0 silenced).You have unapplied migrations; your app may not work properly until they are applied.Run 'python manage.py migrate' to apply them.January 08, 2015 - 15:23:20Django version 1.7.2, using settings 'dj01.settings'Starting development server at http://127.0.0.1:8000/ #如果不指定,默认启动127.0.0.1 8000Quit the server with CONTROL-C.
为了方便测试:
# python manage.py runserver 0.0.0.0:8000 #指定启动ip和端口,0.0.0.0表示所有ip