Django Newbie : “Reverse not found”

情到浓时终转凉″ 提交于 2019-12-03 02:26:20

Do you have a view named viewPlan with which you do something like this in a template:

{% url viewPlan %}

or something like this in a view:

reverse('viewPlan')

If you do that and you do not have a line that looks like this:

url(r'^whatever/url/$', 'dev_env.profiles.views.viewPlan', name="viewPlan"),

...in your url configuration I would imagine that's the error you're getting. Alternatively, and more likely, you are probably capturing a value (maybe id or something) in the viewPlan URL but are not passing an argument when reversing the url. So if you are capturing any values in the regex, like this:

url(r'^plans/(\d+)$', 'dev_env.profiles.views.viewPlan', name="viewPlan"),

You need to call it like this:

{% url viewPlan 15 %}

Or like this:

reverse('viewPlan', args=[15]);

Where 15 is obviously whatever the captured value is expecting.

I had the same issue. In my case, I'd forgotten to add the urls for the child app in the main urls.py file:

urlpatterns = [
    re_path("admin/", admin.site.urls),
    re_path(r"^core/", include("core.urls")),
    re_path(r"^$", welcome, name="welcome")
]

Sometimes need to include the app_label in the name argument

like when define app_name='core' in your core.urls

then reverse the viewPlan path would be:

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