[英]How do I redirect from a view to a root URL in Django?
I'm trying to add a link to my django site that redirects from the catalog1
view to the catalog2
page.我正在尝试添加指向我的
catalog1
站点的链接,该链接从目录 1 视图重定向到catalog2
2 页面。 So basically, I want to go from .../catalog1
to .../catalog2/other-page
.所以基本上,我想 go 从
.../catalog1
到.../catalog2/other-page
。
All the other answers that I've seen require redirecting to http://127.0.0.1:8000/
first, and then redirecting to catalog2
, but I've set up the site so that http://127.0.0.1:8000/
automatically redirects to catalog1
, so this isn't working.我看到的所有其他答案都需要先重定向到
http://127.0.0.1:8000/
,然后再重定向到catalog2
,但我已经设置了站点,以便http://127.0.0.1:8000/
自动重定向到catalog1
,所以这是行不通的。
Assume, a situation, your other-page
is about
,you have about
button in your path http://127.0.0.1:8000/catalog1
假设,一种情况,您的
other-page
是about
,您的路径中有about
按钮http://127.0.0.1:8000/catalog1
catalog1.html catalog1.html
<a href="{% url 'about' %}">about </a>
urls.py网址.py
urlpatterns =[
path('catalog2/about/',views.about,name='about')
]
views.py视图.py
def about(req):
return render(req,'appname/anyfile.html')
So, by clicking on a button about
, it will redirect it to that view which has name='about'
in urls.py
irrespective of any route.因此,通过单击一个按钮
about
,它会将其重定向到urls.py
中具有name='about'
的视图,而不管任何路由。
Then you are redirected to http://127.0.0.1:8000/catelog2/about/
from http://127.0.0.1/8000/catalog1
.然后你被重定向到
http://127.0.0.1:8000/catelog2/about/
从http://127.0.0.1/8000/catalog1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.