Understanding django.shortcuts.redirect

不打扰是莪最后的温柔 提交于 2019-12-01 11:14:24

Why did reverse fail with 'abc' but not with 'abc/'?

Because it interpreted it as a view name (and you indeed have a view named 'abc', see your monitor.urls file). This means Django will call reverse to compute the URL. The value abc/ is interpreted as an actual URL which means Django won't call reverse to determine the URL.

This also explains why reverse failed: the view with name abc also requires an argument called id. Otherwise Django won't be able to lookup the URL as there is no view called abc without parameters.

Based on the documentation you should be able to reverse the URL using:

redirect("abc", id=...)

where ... is the value of the id parameter.

And how does reverse know that the redirect should include monitor/ as well?

That is because it knows what URLs are available and 1) it knows where the view called abc is defined and 2) it knows that monitors.urls is included with monitor/ in front.

What if I had in the main urls.py another app called "xyz" which also has a "abc" view?

In that case you have to use namespaces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!