Skip to main content
 首页 » 编程设计

python之Django之传递 URL "name"属性来查看

2024年08月27日27cloudgamer

我创建了以下 URL:

网址.py

url(r'^authors', GenericView.as_view(model=Author, context_object_name='authors_list', 
     success_url=reverse_lazy('author_link'), 
     template_name='app/authors.html'), name='author_url_name'), 

而且我想访问 View 中的 URL 名称,通过一个变量传递。在这种情况下,'author_url_name'。

我需要的 View 函数如下:

View .py

def get_context_data(self, **kwargs): 
    context = super(AuthorClass, self).get_context_data(**kwargs) 
    context['action'] = reverse_lazy('author_url_name') 
    return context 

这行得通,但我想用某种方法替换“author_url_name”。

感谢您的宝贵时间!!

请您参考如下方法:

https://stackoverflow.com/a/17614086/183948有你的答案。

As of Django 1.5, this can be accessed from the request object

current_url = request.resolver_match.url_name 

https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.HttpRequest.resolver_matc