Django reg exp在urls.py中

时间:2014-02-14 11:41:56

标签: php python regex django

我想做一个有效的字符串URL来呈现模板,我希望有这样的URL:

url(r'main.php?page=04rent&way=1&filtr=1', render_to_some_tempalte)
url(r'main.php?location=%C1%E0%F0%E2%E8%F5%E0&page=04rent&way=1&filtr=1', render_to_some_tempalte)

这些网址,我需要重定向到新模板。这些是来自php服务器的旧网址。

但是这些字符串无效,我怎么能对有效的URL字符串执行此操作,转义字符\没有帮助。

1 个答案:

答案 0 :(得分:1)

URL配置中的路由与查询字符串不匹配。不要使用查询字符串(?和on),而是在Python视图中解析它。

request.GET映射使您可以访问查询字符串参数。仅匹配^main.php$作为路由,并检查视图中的查询参数以确定重定向到何处。从那里使用redirect()

from django.shortcuts import redirect


urlpatterns = patterns('',
    url(r'^main.php$', php_redirects, name='php_main')
)

def php_redirects(request):
    page = request.GET.get('page')
    location = request.GET.get('location')
    # look up URL pattern to redirect to.
    targets = {
        '04rent': 'some_url_pattern_name',
    }
    redirect(target[page], location=location)
相关问题