【问题标题】:Django redirect is appending tuple to URL when used reverseDjango重定向在使用反向时将元组附加到URL
【发布时间】:2021-09-29 14:57:18
【问题描述】:

当我使用success_url = reverse_lazy('weatherdata:sample-list')

它重定向到的 URL 是:

http://localhost:8000/weather/('/weather',)

而不是预期的:

http://localhost:8000/weather/

对于某些上下文。这些是我的模块:

main/urls.py

urlpatterns = [
    path('weather/', include(('weatherdata.urls', 'weatherdata'), namespace="weatherdata")),
    path('admin/', admin.site.urls),
]

天气数据/urls.py

urlpatterns = [
    path('', WeatherSampleList.as_view(), name='sample-list'),
    path('takesample', WeatherByLocationFormView.as_view(), name='take-sample'),
]

在我的 weatherdata/views.py 中

class WeatherByLocationFormView(FormView):
    [...]
    success_url = reverse_lazy('weatherdata:sample-list'),

【问题讨论】:

  • 去掉结尾的逗号。

标签: django django-urls


【解决方案1】:

你有一个杂散的逗号

    success_url = reverse_lazy('weatherdata:sample-list'),

相同
    success_url = (reverse_lazy('weatherdata:sample-list'),)

这是你看到的元组。

做起来

   success_url = reverse_lazy('weatherdata:sample-list')

你是金子。

【讨论】:

  • 谢谢!我知道这一定很傻,但有时你看不到它
猜你喜欢
  • 2015-10-16
  • 2015-03-18
  • 2015-04-24
  • 2021-12-27
  • 2022-01-20
  • 2021-01-12
  • 1970-01-01
  • 2014-12-25
  • 2021-01-16
相关资源
最近更新 更多