如何在django中获取视图函数的url路径

时间:2021-08-27 04:36:51

for example:

例如:

view.py

view.py

def view1( request ):
    return HttpResponse( "just a test..." )

urls.py

urls . py

urlpatterns = patterns('',
    url( r'^view1$', 'app1.view.view1'),
)

some where I have to get the url path of view1 How can I do it? Of course I don't want to hard code the url path "xxx/view1".

我需要获取view1的url路径怎么做呢?当然,我不想硬编码url路径“xxx/view1”。

6 个解决方案

#1


17  

You need reverse.

你需要逆转。

reverse('app1.view.view1')

If you want to find out URL and redirect to it, use redirect

如果您想查找URL并重定向到它,请使用重定向

redirect('app1.view.view1')

If want to go further and not to hardcode your view names either, you can name your URL patterns and use these names instead.

如果想更进一步,也不要硬编码视图名,可以命名URL模式并使用这些名称。

#2


9  

This depends whether you want to get it, if you want to get the url in a view(python code) you can use the reverse function(documentation):

这取决于您是否想获取它,如果您想获取视图(python代码)中的url,您可以使用反向函数(文档):

reverse('admin:app_list', kwargs={'app_label': 'auth'})

And if want to use it in a template then you can use the url tag (documentation):

如果您想在模板中使用它,那么您可以使用url标记(文档):

{% url 'path.to.some_view' v1 v2 %}

#3


3  

If you want the url of the view1 into the view1 the best is request.get_path()

如果希望将view1的url放入view1,最好的方法是request.get_path()

#4


3  

As said by others, reverse function and url templatetags can (should) be used for this.

正如其他人所说,反向函数和url templatetags可以(应该)用于此。

I would recommend to add a name to your url pattern

我建议在您的url模式中添加一个名称。

urlpatterns = patterns('',
    url( r'^view1$', 'app1.view.view1', name='view1'),
)

and to reverse it thanks to this name

多亏了这个名字

reverse('view1')

That would make your code easier to refactor

这会使代码更容易重构

#5


2  

Yes, of course you can get the url path of view named 'view1' without hard-coding the url.

是的,您当然可以获得名为“view1”的视图的url路径,而无需硬编码url。

All you need to do is - just import the 'reverse' function from Django urlresolvers.

只需从Django urlresolvers导入“反向”函数即可。

Just look at the below example code:

请看下面的示例代码:

from django.core.urlresolvers import reverse

from django.http import HttpResponseRedirect

def some_redirect_fun(request):

    return HttpResponseRedirect(reverse('view-name'))

#6


1  

You can use the reverse function for this. You could specify namespaces and names for url-includes and urls respectively, to make refactoring easier.

你可以用反向函数。您可以分别为url包含和url指定名称空间和名称,以使重构更容易。

#1


17  

You need reverse.

你需要逆转。

reverse('app1.view.view1')

If you want to find out URL and redirect to it, use redirect

如果您想查找URL并重定向到它,请使用重定向

redirect('app1.view.view1')

If want to go further and not to hardcode your view names either, you can name your URL patterns and use these names instead.

如果想更进一步,也不要硬编码视图名,可以命名URL模式并使用这些名称。

#2


9  

This depends whether you want to get it, if you want to get the url in a view(python code) you can use the reverse function(documentation):

这取决于您是否想获取它,如果您想获取视图(python代码)中的url,您可以使用反向函数(文档):

reverse('admin:app_list', kwargs={'app_label': 'auth'})

And if want to use it in a template then you can use the url tag (documentation):

如果您想在模板中使用它,那么您可以使用url标记(文档):

{% url 'path.to.some_view' v1 v2 %}

#3


3  

If you want the url of the view1 into the view1 the best is request.get_path()

如果希望将view1的url放入view1,最好的方法是request.get_path()

#4


3  

As said by others, reverse function and url templatetags can (should) be used for this.

正如其他人所说,反向函数和url templatetags可以(应该)用于此。

I would recommend to add a name to your url pattern

我建议在您的url模式中添加一个名称。

urlpatterns = patterns('',
    url( r'^view1$', 'app1.view.view1', name='view1'),
)

and to reverse it thanks to this name

多亏了这个名字

reverse('view1')

That would make your code easier to refactor

这会使代码更容易重构

#5


2  

Yes, of course you can get the url path of view named 'view1' without hard-coding the url.

是的,您当然可以获得名为“view1”的视图的url路径,而无需硬编码url。

All you need to do is - just import the 'reverse' function from Django urlresolvers.

只需从Django urlresolvers导入“反向”函数即可。

Just look at the below example code:

请看下面的示例代码:

from django.core.urlresolvers import reverse

from django.http import HttpResponseRedirect

def some_redirect_fun(request):

    return HttpResponseRedirect(reverse('view-name'))

#6


1  

You can use the reverse function for this. You could specify namespaces and names for url-includes and urls respectively, to make refactoring easier.

你可以用反向函数。您可以分别为url包含和url指定名称空间和名称,以使重构更容易。