简体   繁体   English

Django的URL标签能否显示RedirectView的URL

[英]Can Django's URL tag show the URL of the RedirectView

Let's say that in my urls.py I have a url like this:假设在我的 urls.py 中有一个 url,如下所示:

path("support/", RedirectView.as_view(url="http://www.example.com"), name="support"),

And in one of my templates I use the url tag:在我的一个模板中,我使用了 url 标签:

{% url "support" %}

This of course outputs /support/ as expected.这当然会按预期输出/support/ But what if I want it to output http://www.example.com instead?但是,如果我想让它改为 output http://www.example.com怎么办? Is that at all possible?这可能吗? Skip the redirect basically.基本上跳过重定向。

So <a href="{% url "support" %}">Link</a> would output <a href="http://www.example.com">Link</a> .所以<a href="{% url "support" %}">Link</a>将是 output <a href="http://www.example.com">Link</a>

But what if I want it to render http://www.example.com instead?但是如果我想让它渲染http://www.example.com呢? Is that at all possible?这可能吗? Skip the redirect basically.基本上跳过重定向。

No, in short it is not possible with django views, since the url is of another website and you can't render it in your own.不,简而言之,django 次浏览是不可能的,因为 url 属于另一个网站,您无法在自己的网站中呈现。

You can see what exactly render() does.你可以看到render()到底做了什么。

If you'd like to directly redirect, then simply use anchor tag as:如果您想直接重定向,则只需使用锚标记作为:

<a href="http://www.example.com">Visit website.</a>

You can also do this dynamically, by creating a URLField in one of the models and simply iterating it with href attribute of anchor tag.您也可以通过在其中一个模型中创建URLField并简单地使用锚标记的href属性对其进行迭代来动态地执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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