Django url.py正则表达式无法正常工作

时间:2017-12-07 13:46:54

标签: python regex django python-3.x

我正在尝试从更安全的应用程序中捕获所有URL并将它们发送到catch all视图;例如: http://127.0.0.1:8000/saferdb/123
http://127.0.0.1:8000/saferdb/12

我认为我在url.py中的reg ex存在问题:

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from .models import Question

def index(request):
    latest_question_list = Question.objects.all()[:5]
    output = ', '.join([q.DOT_Number for q in latest_question_list])
    return HttpResponse(output)


def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

Views.py是django教程中的示例代码:

gst_parse_launch()

我注意到/ saferdb /将不起作用,除非reg ex包含斜杠: R' ^ / $'而不是r' ^ $'如django教程中所示。

1 个答案:

答案 0 :(得分:1)

请添加' /'在url的末尾urls.py更安全' app,类似于:

url(r'^saferdb/', include('saferdb.urls', namespace='saferdb'))