{"id":404,"date":"2012-10-19T14:18:28","date_gmt":"2012-10-19T18:18:28","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=404"},"modified":"2012-10-22T13:05:23","modified_gmt":"2012-10-22T17:05:23","slug":"lazy-reverse","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2012\/10\/lazy-reverse\/","title":{"rendered":"Lazy Reverse"},"content":{"rendered":"<p>Working in Django 1.3, I had some trouble with my view code.<\/p>\n<p>Actually, the error was pretty vague, something about my <code>urls.py<\/code> containing no patterns. After much wailing and gnashing of teeth (during which I learned I really don&#8217;t know how to best use the debugger in Eclipse), I traced the problem down to three of my view classes. All three included a setting for the success_url attribute:<\/p>\n<pre><code>success_url = reverse('support_list')\n<\/code><\/pre>\n<p>Apparently, you can&#8217;t do that.<\/p>\n<p>Once I found the offending line of code, something in my mind clicked on to remind me that I had hit this problem before. To solve it, I had moved the reverse statement into the <code>get_success_url()<\/code> function:<\/p>\n<pre><code>def get_success_url()\n    return reverse('support_list')\n<\/code><\/pre>\n<p>Looking for a better solution, a quick Google Search brought up <a href=\"https:\/\/groups.google.com\/forum\/?fromgroups=#!topic\/django-users\/zgk0ycyWxGc\">this post in in the django-users group<\/a>.<\/p>\n<blockquote><p>\n  I have a CreateView which I&#8217;d like to redirect to a custom success_url<\/p>\n<p>  defined in my URLconf. As I want to stick to the DRY-principle I just<\/p>\n<p>  did the following:<\/p>\n<pre><code>  success_url = reverse(\"my-named-url\") \n<\/code><\/pre>\n<p>  Unfortunately, this breaks my site by raising an<\/p>\n<p>  &#8220;ImproperlyConfigured: The included urlconf doesn&#8217;t have any patterns<\/p>\n<p>  in it&#8221;. Removing success_url and setting the model&#8217;s<\/p>\n<p>  get&#95;absolute&#95;url() to the following works fine:<\/p>\n<pre><code>  def get_absolute_url(self): \n      return reverse(\"my-named-url\") \n<\/code><\/pre>\n<p>  I could reproduce this with a brand new project\/application so I don&#8217;t<\/p>\n<p>  think this has something to do with my setup.<\/p>\n<p>  Can anyone confirm this issue?<\/p>\n<\/blockquote>\n<p>So others have hit the same problem. Looking further down the page, a more concise solution was offered:<\/p>\n<pre><code> success_url = lazy(reverse, str)(\"support_list\") \n<\/code><\/pre>\n<p>I used this for my current project &#8211; a little cleaner.<\/p>\n<p>In v1.4, we can use the &#8216;reverse_lazy()&#8217; function in place of &#8216;reverse()&#8217;.<\/p>\n<p><strong>UPDATE:<\/strong><\/p>\n<p>Suddenly, I understand the reason for the <code>SuccessURLRedirectListMixin<\/code> in the popular <a href=\"https:\/\/github.com\/brack3t\/django-braces\">Django Braces<\/a> package.  Using this mixin, one can declare the success url with a simple variable assignment (no lazy code required), and the mixin provides the <code>get_success_url()<\/code> function needed to make everything work.<\/p>\n<p>BTW, I highly recommend using Braces with class based views.  I have found several to be quite useful to speed up coding and to keep things clean and clear.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working in Django 1.3, I had some trouble with my view code. Actually, the error was pretty vague, something about my urls.py containing no patterns. After much wailing and gnashing of teeth (during which I learned I really don&#8217;t know how to best use the debugger in Eclipse), I traced the problem down to three &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2012\/10\/lazy-reverse\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Lazy Reverse&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=404"}],"version-history":[{"count":4,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":406,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/404\/revisions\/406"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}