<p><a href="http://www.djangoproject.com/" rel="noreferrer">Django</a> view points to a function, which can be a problem if you want to change only a bit of functionality. Yes, I could have million keyword arguments and even more if statements in the function, but I was thinking more of an object oriented approach.</p>

<p>For example, I have a page that displays a user. This page is very similar to page that displays a group, but it's still not so similar to just use another data model. Group also has members etc...</p>

<p>One way would be to point views to class methods and then extend that class. Has anyone tried this approach or has any other idea? </p>

[{'answer_id': 764, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Sounds to me like you're trying to combine things that shouldn't be combined.  If you need to do different processing in your view depending on if it's a User or Group object you're trying to look at then you should use two different view functions.</p>\n\n<p>On the other hand there can be common idioms you'd want to extract out of your object&#95;detail type views... perhaps you could use a decorator or just helper functions?</p>\n\n<p>-Dan</p>\n"}, {'answer_id': 4572, 'author': 'rossp', 'author_id': 612, 'author_profile': 'https://Stackoverflow.com/users/612', 'pm_score': 3, 'selected': False, 'text': '<p>If you\'re simply displaying data from models, why not use the <a href="https://docs.djangoproject.com/en/1.2/ref/generic-views/" rel="nofollow noreferrer">Django Generic Views</a>? They\'re designed to let you easy show data from a model without having to write your own view and stuff about mapping URL paramaters to views, fetching data, handling edge cases, rendering output, etc.</p>\n'}, {'answer_id': 8320, 'author': 'Harley Holcombe', 'author_id': 1057, 'author_profile': 'https://Stackoverflow.com/users/1057', 'pm_score': 2, 'selected': False, 'text': '<p>Unless you want to do something a little complex, using the generic views are the way to go.  They are far more powerful than their name implies, and if you are just displaying model data generic views will do the job.</p>\n'}, {'answer_id': 27780, 'author': 'Andrew Wilkinson', 'author_id': 2990, 'author_profile': 'https://Stackoverflow.com/users/2990', 'pm_score': 1, 'selected': False, 'text': '<p>If you want to share common functionality between pages I suggest you look at custom tags. They\'re quite <a href="https://docs.djangoproject.com/en/1.1/howto/custom-template-tags/" rel="nofollow noreferrer">easy to create</a>, and are very powerful.</p>\n\n<p>Also, <a href="https://code.djangoproject.com/wiki/ExtendingTemplates" rel="nofollow noreferrer">templates can extend from other templates</a>. This allows you to have a base template to set up the layout of the page and to share this between other templates which fill in the blanks. You can nest templates to any depth; allowing you to specify the layout on separate groups of related pages in one place.</p>\n'}, {'answer_id': 27792, 'author': 'dguaraglia', 'author_id': 2384, 'author_profile': 'https://Stackoverflow.com/users/2384', 'pm_score': 2, 'selected': False, 'text': '<p>You can always create a class, override the <em><code>__call__</code></em> function and then point the URL file to an instance of the class. You can take a look at the <a href="http://code.djangoproject.com/browser/django/trunk/django/contrib/formtools/wizard.py" rel="nofollow noreferrer">FormWizard</a> class to see how this is done.</p>\n'}, {'answer_id': 33957, 'author': 'Carl Meyer', 'author_id': 3207, 'author_profile': 'https://Stackoverflow.com/users/3207', 'pm_score': 7, 'selected': True, 'text': '<p>I\'ve created and used my own generic view classes, defining <strong><code>__call__</code></strong> so an instance of the class is callable.  I really like it; while Django\'s generic views allow some customization through keyword arguments, OO generic views (if their behavior is split into a number of separate methods) can have much more fine-grained customization via subclassing, which lets me repeat myself a lot less.  (I get tired of rewriting the same create/update view logic anytime I need to tweak something Django\'s generic views don\'t quite allow).</p>\n\n<p>I\'ve posted some code at <a href="http://www.djangosnippets.org/snippets/1009/" rel="noreferrer">djangosnippets.org</a>.</p>\n\n<p>The only real downside I see is the proliferation of internal method calls, which may impact performance somewhat. I don\'t think this is much of a concern; it\'s rare that Python code execution would be your performance bottleneck in a web app.</p>\n\n<p><strong>UPDATE</strong>: Django\'s own <a href="http://docs.djangoproject.com/en/dev/topics/class-based-views/" rel="noreferrer">generic views</a> are now class-based.</p>\n\n<p><strong>UPDATE</strong>: FWIW, I\'ve changed my opinion on class-based views since this answer was written. After having used them extensively on a couple of projects, I feel they tend to lead to code that is satisfyingly DRY to write, but very hard to read and maintain later, because functionality is spread across so many different places, and subclasses are so dependent on every implementation detail of the superclasses and mixins. I now feel that <a href="https://docs.djangoproject.com/en/dev/ref/template-response/" rel="noreferrer">TemplateResponse</a> and view decorators is a better answer for decomposing view code.</p>\n'}, {'answer_id': 123090, 'author': 'Andrew Ingram', 'author_id': 15687, 'author_profile': 'https://Stackoverflow.com/users/15687', 'pm_score': 1, 'selected': False, 'text': "<p>Generic views will usually be the way to go, but ultimately you're free to handle URLs however you want. FormWizard does things in a class-based way, as do some apps for RESTful APIs.</p>\n\n<p>Basically with a URL you are given a bunch of variables and place to provide a callable, what callable you provide is completely up to you - the standard way is to provide a function - but ultimately Django puts no restrictions on what you do.</p>\n\n<p>I do agree that a few more examples of how to do this would be good, FormWizard is probably the place to start though.</p>\n"}, {'answer_id': 2921293, 'author': 'Erik Allik', 'author_id': 351981, 'author_profile': 'https://Stackoverflow.com/users/351981', 'pm_score': 4, 'selected': False, 'text': '<p>I needed to use class based views, but I wanted to be able to use the full name of the class in my URLconf without always having to instantiate the view class before using it. What helped me was a surprisingly simple metaclass:</p>\n\n<pre><code>class CallableViewClass(type):\n    def __call__(cls, *args, **kwargs):\n        if args and isinstance(args[0], HttpRequest):\n            instance = super(CallableViewClass, cls).__call__()\n            return instance.__call__(*args, **kwargs)\n        else:\n            instance = super(CallableViewClass, cls).__call__(*args, **kwargs)\n            return instance\n\n\nclass View(object):\n    __metaclass__ = CallableViewClass\n\n    def __call__(self, request, *args, **kwargs):\n        if hasattr(self, request.method):\n            handler = getattr(self, request.method)\n            if hasattr(handler, \'__call__\'):\n                return handler(request, *args, **kwargs)\n        return HttpResponseBadRequest(\'Method Not Allowed\', status=405)\n</code></pre>\n\n<p>I can now both instantiate view classes and use the instances as view functions, OR I can simply point my URLconf to my class and have the metaclass instantiate (and call) the view class for me. This works by checking the first argument to <code>__call__</code> – if it\'s a <code>HttpRequest</code>, it must be an actual HTTP request because it would be nonsense to attept to instantiate a view class with an <code>HttpRequest</code> instance.</p>\n\n<pre><code>class MyView(View):\n    def __init__(self, arg=None):\n        self.arg = arg\n    def GET(request):\n        return HttpResponse(self.arg or \'no args provided\')\n\n@login_required\nclass MyOtherView(View):\n    def POST(request):\n        pass\n\n# And all the following work as expected.\nurlpatterns = patterns(\'\'\n    url(r\'^myview1$\', \'myapp.views.MyView\', name=\'myview1\'),\n    url(r\'^myview2$\', myapp.views.MyView, name=\'myview2\'),\n    url(r\'^myview3$\', myapp.views.MyView(\'foobar\'), name=\'myview3\'),\n    url(r\'^myotherview$\', \'myapp.views.MyOtherView\', name=\'otherview\'),\n)\n</code></pre>\n\n<p>(I posted a snippet for this at <a href="http://djangosnippets.org/snippets/2041/" rel="noreferrer">http://djangosnippets.org/snippets/2041/</a>)</p>\n'}, {'answer_id': 26250049, 'author': 'Varun Chadha', 'author_id': 3408904, 'author_profile': 'https://Stackoverflow.com/users/3408904', 'pm_score': 1, 'selected': False, 'text': '<p>You can use the Django Generic Views. You can easily achieve desired functionality thorough Django generic Views</p>\n'}]
['https://Stackoverflow.com/questions/742', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/189/']