Django开发的URL的基本配置

来源:互联网 发布:linux shell 等待几秒 编辑:程序博客网 时间:2024/05/17 03:13

最近在做Python Django的开发。过程中遇到的困难主要是在开始阶段配置url的时候。现在做一下总结:
参考资料:
http://www.nowamagic.net/academy/detail/13281005
http://www.nowamagic.net/academy/detail/13281014
http://www.nowamagic.net/academy/detail/13281030
http://www.cnblogs.com/fnng/p/3750596.html

下面是一个典型的url.py的代码

from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:# from django.contrib import admin# admin.autodiscover()urlpatterns = patterns('',    # Examples:    # url(r'^$', 'nowamagic.views.home', name='home'),    # url(r'^nowamagic/', include('nowamagic.foo.urls')),    # Uncomment the admin/doc line below to enable admin documentation:    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),    # Uncomment the next line to enable the admin:    # url(r'^admin/', include(admin.site.urls)),)

模式包含了一个尖号(^)和一个美元符号()hello/匹配hello/字符串,即在网址http://127.0.0.1:8000/hello/找到hello/后,使用hello()函数显示出来,如果没有’$’结尾,则网址中输入hello1/;hello2/都会对应以hello()函数显示出来。

from django.http import HttpResponse,Http404def hello(request):     #每个视图函数至少要有一个参数,通常被叫作request。     return HttpResponse("Hello NowaMagic!") #一个视图功能必须返回一个HttpResponse
(r'^$', index),

index 函数就是生成首页的 view 函数
在 view 函数里,return HttpResponseRedirect(‘../’):返回主页,即127.0.0.1。

0 0
原创粉丝点击