python - Django - 将 URL "name"属性传递给 View

标签 python django view django-urls

我创建了以下 URL:

urls.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 函数如下:

views.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

关于python - Django - 将 URL "name"属性传递给 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25044066/

相关文章:

python - 如何使用 django-allauth 创建多个注册页面?

python - 通过返回非预定义字段。 Django 中的 Tastypie API

javascript - backbone js - 动态加载 View : should el be the launcher, 还是应该由父 View 启动?

sql - 编写查询的更好方法

获取序列的第一个和最后一个元素的 Pythonic 方法

python - 如何使用静态用户 ID 和密码将需要登录的装饰器集成到 flask 路由中?

python - 随机排列 DataFrame 的行,直到列中的所有连续值都不同?

python - 如何在没有 pyinstaller 的情况下使用 "./"创建 python 可执行文件

django - django 网站的流程是什么…… View 、模板、模型和表单实际上做了什么

ios - 无论文本长度如何,都可以快速对齐 TextView 下方的按钮