Python 带有参数的django重定向视图
在我的应用程序中,我的文件urls.py包含以下行:Python 带有参数的django重定向视图,python,django,Python,Django,在我的应用程序中,我的文件urls.py包含以下行: ... path('home/', views.home, name='home'), path('page/', views.page, name='page'), .... 在my view.py文件中,我有两个视图,如下所示: def home(request, value=0): print("value=", value) return render(request, 'template.html', contex
...
path('home/', views.home, name='home'),
path('page/', views.page, name='page'),
....
在my view.py文件中,我有两个视图,如下所示:
def home(request, value=0):
print("value=", value)
return render(request, 'template.html', context)
def page(request):
if bad:
return redirect(reverse('home'), value=1)
是否可以使用带有参数(如本例中的值)的view函数,然后使用URL.py中描述的格式从页面视图重定向,并根据相同的条件(如value=1)传递一些值?
无论发生什么情况,上面的代码总是打印value=0
我能想到的唯一方法是使用全局变量,我真的很想避免…是的,但您需要将参数添加到URL:
path('home/', views.home, name='home1'),
path('home/<int:value>/', views.home, name='home2'),
我不能在url.py文件中使用
path('home/',views.home,name='home'),
?我是否需要像您的示例中那样的两条路径?该死如果这是我认为我将使用globals的唯一方法…@idkasd:大多数web服务器不使用数据库(和一些缓存)以外的持久性内容,那么使用全局状态()将是一种严重的反模式,并且会使应用程序立即无法扩展。但是,通常HTTP协议是无状态的,因此这意味着您应该将数据传递给客户端,让它将数据传递回您。
def page(request):
if bad:
return redirect('home2', value=1)