【发布时间】:2021-09-22 16:00:00
【问题描述】:
我试图在 Django 的 urlpatterns 中获取 url 的变量和值。我的意思是,我想输入浏览器类型的地址:https://place.com/url=https://www.google.es/..。能够做翻译。并且能够在接收到的函数中提取变量和值。目前我正在尝试使用这样的 re_path 来获取它:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.index),
re_path('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', views.index_traductor),
]
正则表达式匹配将其拾取,但我不知道如何将其作为变量中的值发送以在此处接收:
from django.http import HttpResponse
def index(request):
return HttpResponse("flag")
def index_traductor(request, url=''):
return HttpResponse("%s" % url)
我得到一个空白页。有什么想法吗?
【问题讨论】: