Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example of a reusable Django View
# This is not tested and not complete, but give an interesting example of how you can turn a Django view into something reusable.
# It is based on the code here:
# It might make sense to turn this into an abstract base class.
# The purpose of this is:
# 1. Improved code reuse for outputting data.
# 2. Easier modification to relevant areas
# 3. Because it is new and interesting to me (Kiss my future jobs goodbye for admitting that :/)
# 4. Can stash it in a different file, so the view is not as cluttered-up.
# For example, you could define a class JsonExchange(ChatExchange) and make small modifications to output as JSON,
# an XMLExchange(ChatExchange) to do the same for XML, etc... It seems far more reusable, although I could be wrong.
class ChatExchange(object):
def authorize(self, request):
# This is how we normally authorize the chat request.
return request.user.is_authenticated()
def invalid_login(self):
return HttpResponseBadRequest('You need to be logged in to access the chat system.')
def create_conversation(self, user1, user2):
c = Conversation() = "Conversation between %s and %s" % (str(user1), str(user2))
return c
# This retrieves the conversation
def get_conversation(self):
user1 =
user2 = user.objects.get(username=self.request.POST.get("to"))
# find a conversation that has user1 and user2
conversation = Conversation.objects.filter(
self.conversation = conversation
elif Conversation.can_start_conversation(user1, user2):
self.conversation = conversation
if Conversation.can_start_conversation(user1, user2):
self.conversation = self.create_conversation(user1, user2)
self.conversation = False
# This adds messages to the conversation.
def add_messages(self):
if "new_message" not in self.request.POST:
if not self.conversation:
user1 = self.request.POST.get("userid")
m = Message()
m.user = user1
m.conversation = self.conversation
m.text = self.request.POST.get("message")
self.message = m
self.statusCode = 2
# This retrieves the messages in the conversation
def get_messages(self):
if not self.conversation:
self.messages = Message.objects.filter(conversation=self.conversation)
self.statusCode = 1
# An empty function that you can define to filter messages or perform any transformations necessary before output is made.
def prehook(self):
# An empty function that you can define to filter messages or perform any transformations necessary before output is made.
def posthook(self):
# Outputs messages in the format defined. Must be overridden.
def output_messages(self):
raise NotImplementedError( "Should have implemented this" )
# Outputs a success message if you want to override something or other.
def output_success_message(self):
raise NotImplementedError( "Should have implemented this" )
# Outputs a list of errors in the program.
def output_errors(self):
raise NotImplementedError( "Should have implemented this" )
def __call__(self, request):
if not self.authorize(request):
return self.invalid_login(request)
self.statusCode = 0
self.request = request
if self.statusCode == 1:
return self.output_messages()
if self.statusCode == 2:
return self.output_success_message()
return self.output_errors()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.