【问题标题】:Is it possible to set 2 urls in get_absolute_url?是否可以在 get_absolute_url 中设置 2 个 url?
【发布时间】:2017-08-24 06:17:42
【问题描述】:

我有下一个任务,不知道怎么做。

网页向用户显示对象列表。每个对象都有 2 个按钮,其 URL 为 function_detailfunction_change_history。第一个 url 需要打开关于对象详细信息的页面,第二个 url 需要打开关于更改历史的页面。我使用get_absolute_url 创建第二个网址,但不知道如何创建第一个网址,因为我需要再次使用get_absolute_url

models.py:

@reversion.register()
class Function(models.Model):
    ***FIELDS***

    def get_absolute_url(self):
        return reverse('project:function_change_history', args=[self.project_id, self.code])

urls.py:

url(r'^(?P<project_code>[0-9a-f-]+)/(?P<function_code>[0-9a-f-]+)/change_history/$',
        function_change_history,
        name='function_change_history'),

url(r'^(?P<project_code>[0-9a-f-]+)/(?P<function_code>[0-9a-f-]+)/detail/$',
        function_detail,
        name='function_detail'),

可以在get_absolute_url中设置2 url吗?

【问题讨论】:

  • 这没有意义。为什么需要它们在 get_absolute_url 中?
  • 好吧,我使用get_absolute_url,因为我需要有关特定对象的信息。
  • 这根本不是答案。为什么是这种方法,具体来说?你如何使用它?为什么需要两个结果?

标签: python django python-3.x django-1.10


【解决方案1】:

我终于找到了决定。对于function_detail,我使用get_absolute_url,对于function_change_history,我在模板中使用{% url 'function_change_history' project_code=project.code function_code=function.code %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2020-06-21
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2011-09-06
    相关资源
    最近更新 更多