Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question seems to be for older versions of Django. I'm not familiar with how the old generic views work. But the new class-based generic views have the same problem.</p> <p>Reversing doesn't work "out of the box" because View.as_view() returns a different wrapper function each time, and they don't compare equal to each other, so reverse() can't find the reverse route by comparing two functions that aren't equal.</p> <p>There is another way, although it's non-standard. This is what I do for my class-based views:</p> <pre><code>class OrderView(LoginRequiredMixin, CreateView): model = Order form_class = OrderForm OrderView.plain_view = staticmethod(OrderView.as_view()) </code></pre> <p>In this case, I use <code>plain_view</code> to mean the view returned by <code>as_view()</code> with no arguments. If you pass arguments to <code>as_view()</code>, then the wrapper it returns will actually be different to the plain one. So if you need both, you'd have to assign them to different properties:</p> <pre><code>OrderView.plain_view = staticmethod(OrderView.as_view()) OrderView.bonk_view = staticmethod(OrderView.as_view(whee='bonk')) </code></pre> <p>You can link to these view attributes in <code>urls.py</code>:</p> <pre><code>urlpatterns = patterns('', url(r'^order/$', views.OrderView.plain_view), url(r'^frob/$', views.OrderView.bonk_view), </code></pre> <p>and then you can reverse them by reversing the view attributes:</p> <pre><code>def get_success_url(self): return reverse(OrderView.plain_view) def get_failure_url(self): return reverse(OrderView.bonk_view) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload