This one has me scratching my head. I have an app with views that do form processing (logins/signup) and then return various HttpResponseRedirect()s based on the input. All of those redirects contain reverse() lookups with the appropriate functions listed as strings. And every function has a corresponding urlpattern in urls.py.
Everything was working fine until this morning.
Now, whenever I submit a form, Django gives me a syntax error for a non-existent line:
SyntaxError at /logout/
invalid syntax (views.py, line 399)
(That file only has 354 lines)
When I scroll down to look at the traceback, the line that’s highlighted is always one with a return HttpResponseRedirect( reverse('app.views.func') ).
Because of these bewildering error messages, I’m not even sure that the problem is really with the HttpResponseRedirect( reverse() )s. I haven’t touched any of that code in a few days, so I’m not sure why it would suddenly start throwing out weird errors like that.
Any help debugging this would be much appreciated!
I finally figured it out after consulting the docs for the
reverse()function.When you call
reverse(), django first imports your project’s URLConf files, which in turn imports every single view module that is declared in your URLconf. My issue was that I was working on a new, totally unrelated view that had a syntax error (on line 399!).So even though I wasn’t viewing a page that was doing anything with the new view, my old view was still getting tripped up with the syntax error because of how
reverse()works.From the docs: