Django 视图,在 1 个视图中有 2 个重定向
Django Views, having 2 redirects in 1 view
是否可以在同一个 Django 视图中使用 2 个 redirect()。所以当点赞按钮在主页时,我希望它重定向回主页,如果点赞按钮在详细页面,我想重定向回详细页面?
例如:
def LikeView(request, slug):
context = {}
post = get_object_or_404(BlogPost, slug=slug)
post.likes.add(request.user)
if in homepage:
return redirect('HomeFeed:detail', slug=slug)
else:
return redirect('HomeFeed:main')
def delete_blog_view(request,slug):
context = {}
user = request.user
#YOU WANT to check if user is authenticated. if not, you need to authenticate! redirect you to that page
if not user.is_authenticated:
return redirect("must_authenticate")
account = Account.objects.get(pk=user_id)
blog_post = get_object_or_404(BlogPost, slug=slug)
blog_post.delete()
return redirect(request.META.get('HTTP_REFERER', 'account:view', kwargs={'user_id': account.pk }))
在 next
URL 参数中传递重定向 URL。像这样:
<!-- In homepage template -->
<a href="{% url 'link-to-like-view' %}?next=/home/">Like</a>
<!-- in Detail template -->
<a href="{% url 'link-to-like-view' %}?next=/detail/">Like</a>
或简单地:
<a href="{% url 'link-to-like-view' %}?next={{ request.path }}">Like</a>
始终传递当前 URL 作为重定向 URL。
然后在你的 LikeView
:
def LikeView(request, slug):
...
next = request.GET.get("next", None)
if next and next != '':
return HttpResponseRedirect(redirect_to=next)
# Then have a default redirect URL in case `next` wasn't passed in URL (Home for Example):
return HttpResponseRedirect(redirect_to="/home")
如 Django 文档中所述,这不安全(对于大多数应用程序),因此您必须检查 URL 是否安全然后重定向到 next
否则只是 return 默认安全应用内 URL.
阅读 url_has_allowed_host_and_scheme
函数以检查 URL 在 this Docs page
上是否安全
是否可以在同一个 Django 视图中使用 2 个 redirect()。所以当点赞按钮在主页时,我希望它重定向回主页,如果点赞按钮在详细页面,我想重定向回详细页面?
例如:
def LikeView(request, slug):
context = {}
post = get_object_or_404(BlogPost, slug=slug)
post.likes.add(request.user)
if in homepage:
return redirect('HomeFeed:detail', slug=slug)
else:
return redirect('HomeFeed:main')
def delete_blog_view(request,slug):
context = {}
user = request.user
#YOU WANT to check if user is authenticated. if not, you need to authenticate! redirect you to that page
if not user.is_authenticated:
return redirect("must_authenticate")
account = Account.objects.get(pk=user_id)
blog_post = get_object_or_404(BlogPost, slug=slug)
blog_post.delete()
return redirect(request.META.get('HTTP_REFERER', 'account:view', kwargs={'user_id': account.pk }))
在 next
URL 参数中传递重定向 URL。像这样:
<!-- In homepage template -->
<a href="{% url 'link-to-like-view' %}?next=/home/">Like</a>
<!-- in Detail template -->
<a href="{% url 'link-to-like-view' %}?next=/detail/">Like</a>
或简单地:
<a href="{% url 'link-to-like-view' %}?next={{ request.path }}">Like</a>
始终传递当前 URL 作为重定向 URL。
然后在你的 LikeView
:
def LikeView(request, slug):
...
next = request.GET.get("next", None)
if next and next != '':
return HttpResponseRedirect(redirect_to=next)
# Then have a default redirect URL in case `next` wasn't passed in URL (Home for Example):
return HttpResponseRedirect(redirect_to="/home")
如 Django 文档中所述,这不安全(对于大多数应用程序),因此您必须检查 URL 是否安全然后重定向到 next
否则只是 return 默认安全应用内 URL.
阅读 url_has_allowed_host_and_scheme
函数以检查 URL 在 this Docs page