django url regex不匹配

时间:2011-05-03 13:43:49

标签: regex django django-urls

将Django 1.3与开发服务器一起使用

我尝试连接:http://127.0.0.1:8000/lang/en

答案是:

  

使用中定义的URLconf   pruebas.urls,Django尝试了这些URL   模式,按此顺序:

     
      
  1. ^管理员/
  2.   
  3. ^邮报/ $
  4.   
  5. ^登录/ $
  6.   
  7. ^郎/(ΔP\ W +)/ $
  8.   
  9. ^ site_static /(ΔP。*)$
  10.         

    当前网址与任何网址都不匹配   这些

这是我的“urls.py”:

from pruebas import settings
from django.conf.urls.defaults import *


# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'pruebas.views.home', name='home'),
    # url(r'^pruebas/', include('pruebas.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)),
    (r'^correo/$', 'mailclient.views.index'),
    (r'^login/$', 'kusers.views.klogin'),

    # Language change
    (r'^lang/(?P<lang_code>\w+)/$', 'kusers.views.lang'),    

)




if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^site_static/(?P<path>.*)$', 'django.views.static.serve',  
         {'document_root':     settings.STATIC_ROOT}),)

我认为这一行:

(r'^ lang /(?P \ w +)/ $','kusers.views.lang'),

会与“http://127.0.0.1:8000/lang/en”匹配,但似乎我错了。

提前致谢

kusers / views.py中的代码是:

def lang(request, lang_code):
    request.session['django_language'] = lang_code
    return HttpResponseRedirect( "/" )

解决。

问题不是urls.py配置。问题是'kuser'app文件夹estructure。

由于

1 个答案:

答案 0 :(得分:3)

如果你在网址上加上一个斜杠,它就会起作用,如:

http://127.0.0.1:8000/lang/en/