I had the following code:
When the code above runs, an error is thrown:
There are two solutions to this problem, solution one:
According to Django's document, reverse_lazy should be used instead of reverse when your project's URLConf is not loaded. The documentation specifically points out that reverse_lazy should be used in the following situation:
providing a reversed URL as the url attribute of a generic class-based view. (this is the situation I encountered)
providing a reversed URL to a decorator (such as the login_url argument for the django.contrib.auth.decorators.permission_required() decorator).
providing a reversed URL as a default value for a parameter in a function’s signature.
It is unclear when URLConf is loaded. At least I cannot find the documentation on this topic. So if the above error occurs again, try reverse_lazy