title: Resource views in Django
body: <p>I'm using Django for the first time (I'm also new to Python in general) and I've been struggling with a certain problem related to class based views for a while now. I'm coming from PHP with Laravel, where you can have so-called "RESTful Resource Controllers".</p>
  
  <p>For everyone not familiar with Laravel: the idea is that you have one controller (or view, in Django-speak) per 'resource'. This controller/view defines methods such as <code>index()</code>, <code>show()</code>, <code>create()</code>, <code>store()</code>, <code>edit()</code>, etc. The associated urls and HTTP methods for these controller methods are <code>GET /photos</code>, <code>GET /photos/1</code>, <code>GET /photos/create</code>, <code>POST /photos</code>, <code>GET /photos/1/edit</code> respectively (for example, if you're dealing with photos). In Laravel's routing, you have to declare just one line <code>Route::resource('photos', 'PhotoController')</code> and those URLs are automatically generated. For more info, I refer to <a href="https://laravel.com/docs/5.3/controllers#resource-controllers" rel="nofollow noreferrer">Laravel's docs</a>.</p>
  
  <p>I really like this pattern and I'd like to use it in Django.</p>
  
  <p>As far as I understand, Django doesn't really have such a thing out of the box. Django does have class based views, but those don't represent resources. Rather, you have a view for your 'index()' (<code>PhotoListView</code>), a view for your 'show()' (<code>PhotoDetailView</code>) and so on.</p>
  
  <p>I'm not sure how I should implement this pattern in Django, and I'm not even sure if what I want is a good idea.</p>
  
  <p>After researching I found some info that might be of use:</p>
  
  <p><a href="http://watchitlater.com/blog/2010/02/django-restful-resources/" rel="nofollow noreferrer">http://watchitlater.com/blog/2010/02/django-restful-resources/</a><br>
  <a href="https://baxeico.wordpress.com/2014/06/25/put-and-delete-http-requests-with-django-and-jquery/" rel="nofollow noreferrer">https://baxeico.wordpress.com/2014/06/25/put-and-delete-http-requests-with-django-and-jquery/</a></p>
  
  <p>I'm not sure what the custom View class from the first link accomplishes that the stock Django View can't (a normal Django View does more or less the same thing, right?), and I don't think the second link does exactly what I want (if I understand correctly, the article merely describes a middleware as a solution for the problem that Django doesn't put data sent with a DELETE/PUT request in a request).</p>
  
  <p>Based on this, I devised two potential solutions:</p>
  
  <ul>
  <li><p>Use a single line in the URLconf that matches all URLs for a certain resource, like this:</p>
  
  <p><code>url(r'^photos/(.*)$', PhotoView.as_view())</code></p>
  
  <p>The <code>as_view()</code> function of the custom View base class would then handle the routing of the URLs like <code>/photos</code>, <code>/photos/1</code> and <code>/photos/1/edit</code> and so on to the correct class methods. The disadvantage is that you put routing in a view (not the correct place to do that) and that you can't use named patterns this way to refer to the URLs elsewhere in your code. It is closest to the way its works in Laravel though.</p></li>
  <li><p>Use separate lines in the URLconf for each URL, like:</p>
  
  <p><code>url(r'^photos$', PhotoView.index)
  url(r'^photos/(\d+)$', PhotoView.show)
  url(r'^photos/(\d+)/edit$', PhotoView.edit)</code></p>
  
  <p>The benefit of this is that named patterns work as usual, and all the routing stays in the URLconf. However, I don't know how to accomplish this in terms of implementation of the View class (I'd have to decorate every method @classonlymethod, right).</p></li>
  </ul>
  
  <p>Sorry for the wall of text, I'd gladly hear your thoughts on how to best solve this. Or maybe I'm just batshit crazy and should I just use function based views like every normal Django coder?</p>