简体   繁体   English

如何从视图重定向到 Django 中的根 URL?

[英]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-pageabout ,您的路径中有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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM