liuken@liuken-MS-7798:~$ python manage.py runserver
python: can't open file 'manage.py': [Errno 2] No such file or directoryliuken@liuken-MS-7798:~$ cd /usr/local/bin
liuken@liuken-MS-7798:/usr/local/bin$ python manage.py runserver
python: can't open file 'manage.py': [Errno 2] No such file or directory
liuken@liuken-MS-7798:/usr/local/bin$ ls
cftp django-admin.py manhole tap2deb tkconch
ckeygen lore pyhtmlizer tap2rpm trial
conch mailmail scrapy tapconvert twistd
liuken@liuken-MS-7798:/usr/local/bin$ cd ..
liuken@liuken-MS-7798:/usr/local$ cd ..
liuken@liuken-MS-7798:/usr$ cd ..
liuken@liuken-MS-7798:/$ cd home
liuken@liuken-MS-7798:/home$ cd liuken
liuken@liuken-MS-7798:~$ cd djcode
liuken@liuken-MS-7798:~/djcode$ cd mysite
liuken@liuken-MS-7798:~/djcode/mysite$ python manage.py runserver
Validating models...
0 errors found
July 31, 2013 - 07:04:05
Django version 1.5.1, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
/usr/local/lib/python2.7/dist-packages/django/conf/urls/defaults.py:3: DeprecationWarning: django.conf.urls.defaults is deprecated; use django.conf.urls instead
DeprecationWarning)
[31/Jul/2013 07:04:21] "GET / HTTP/1.1" 404 2002
[31/Jul/2013 07:04:59] "GET /hello HTTP/1.1" 301 0
[31/Jul/2013 07:04:59] "GET /hello/ HTTP/1.1" 200 11
http://127.0.0.1:800
开发服务器的地址是 http://127.0.0.1:8000/ ,打开你的浏览器访问 http://127.0.0.1:8000/hello/ 。 你就可以看到输出结果了。 开发服务器将自动检测Python代码的更改来做必要的重新加载
输出结果:
Hello world
向做到这些,你必须:
你需要输入到views.py文件:
from django.http import HttpResponse def hello(request): return HttpResponse("Hello world")
urls.py 文件
from django.conf.urls.defaults import * from mysite.views import hello urlpatterns = patterns('', ('^hello/$', hello), )
我们做了两处修改。
-
首先,我们从模块 (在 Python 的 import 语法中, mysite/views.py 转译为 mysite.views ) 中引入了 hello 视图。 (这假设mysite/views.py在你的Python搜索路径上。关于搜索路径的解释,请参照下文。)
4
-
接下来,我们为urlpatterns加上一行:(‘^hello/$’, hello),这行被称作URLpattern,它是一个Python的元组。元组中第一个元素是模式匹配字符串(正则表达式);第二个元素是那个模式将使用的视图函数。
简单来说,我们只是告诉 Django,所有指向 URL /hello/ 的请求都应由 hello 这个视图函数来处理。