[英]Redirecting a View to another View in Django Python
How does one redirect from one View to another (next-going one):如何从一个视图重定向到另一个视图(下一个):
class FooView(TemplateView):
template_name 'foo.html'
def post(self, *args, **kwargs):
return redirect(BarView)
# return redirect(BarView.as_view()) ???
class BarView(TemplateView):
template_name 'bar.html'
Give the URL pattern itself a name in your urls.py: 在urls.py中为URL模式本身指定一个名称:
url('/bar/', BarView.as_view(), name='bar')
and just pass it to redirect: 并将其传递给重定向:
return redirect('bar')
如果您在urls.py
为视图指定了名称,则可以使用重定向 。
return redirect('name-of-view-in-urls-py'))
You need to give the view name "bar" to the path in "myapp/urls.py" as shown below:您需要将视图名称“bar”赋予“myapp/urls.py”中的路径,如下所示:
# "myapp/urls.py"
from django.urls import path
from . import views
app_name = "myapp"
urlpatterns = [ # This is view name
path('bar/', views.BarView, name="bar")
]
Then, you need the conbination of the app name "myapp", colon ":" and the view name "bar" as shown below.然后,您需要应用程序名称“myapp”、冒号“:”和视图名称“bar”的组合,如下所示。 In addition, you don't need to import the view "bar" in "myapp/views.py" :
此外,您不需要在“myapp/views.py”中导入视图“bar ”:
# "myapp/views.py"
from django.shortcuts import redirect
def FooView(request):
# Here
return redirect("myapp:bar")
def BarView(request):
return render(request, 'myapp/index.html', {})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.