首页 > 解决方案 > Django重定向在使用反向时将元组附加到URL

问题描述

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

它重定向到的 URL 是:

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

而不是预期的:

http://localhost:8000/天气/

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

主/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'),

标签: djangodjango-urls

解决方案


你有一个杂散的逗号

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

这与

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

这是你看到的元组。

做了

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

你是金色的。


推荐阅读