【问题标题】:URL reversing not working in django when using arguments使用参数时 URL 反转在 django 中不起作用
【发布时间】:2013-05-18 23:47:58
【问题描述】:

我真的很喜欢这个。

我的 urlpatterns 中有以下内容:

url(r'^user/$', UserView.as_view(), name='farmauth-user'),
url(r'^user/(?P<id>\d+)/confirm/(?P<token>\w+)/$', UserConfirmationView.as_view(), name='farmauth-confirm'),

我也有其他网址。然后在我的 HTML 中得到

Reverse for 'farmauth-confirm' with arguments '([u'28'], [u'n48DsSASbKhabWXzZ6XV'])' and keyword arguments '{}' not found.

当我这样做时:

{% url 'farmauth-confirm' id token %}

我也尝试过使用位置参数。如果您想知道,URL 在我尝试这样做的地方是可见的,因为这很有效:

{% url 'farmauth-user' %}

我尝试了带参数和不带参数的其他 URL。使用参数时它永远不会起作用。我做错了什么??

请指教。

【问题讨论】:

  • 那一刻我花了好几个小时试图弄清楚,一旦我发了帖子,我就做对了 -.-;将参数传递给我的视图时,我正在这样做: return render(request, 'confirm.html', dict(self.get_form_context(), **request.GET)) 它接缝 GET 参数是数组,所以令牌和ID 以 [id] 和 [token] 形式给出。现在我这样做: kwargs = { 'id': request.GET.get('id'), 'token': request.GET.get('token'), } return render(request, 'confirm.html', dict(self.get_form_context(), **kwargs)) 它的工作原理。
  • 我还不能作为答案发布。我稍后再做。
  • 看起来你已经明白了,但你的 url 的 id 类型为 integer,但 url 正在评估 id 作为 unicode。因此出现错误。

标签: django django-templates django-views


【解决方案1】:

试试另一个订单:
url(r'^user/(?P\d+)/confirm/(?P\w+)/$', UserConfirmationView.as_view(), name='farmauth-confirm'),
url(r'^user/$', UserView.as_view(), name='farmauth-user'),

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 2016-12-27
    • 2011-11-03
    • 1970-01-01
    • 2012-08-07
    • 2012-02-21
    • 2016-05-14
    • 2021-08-28
    相关资源
    最近更新 更多