I’m developing an app with django, and I have a view where I use 2 return render_to_response, with two different html files, depending on the status of the user.
I was wondering if it would be a better practice to split my view into two different views or if I should keep a bigger view.
What are the pros and the cons of doing so?
Sorry if my question is not clear. Thank you very much for your advices.
There’s no right or wrong answer to this question so your question may not be acceptable on stackoverflow, which usually is intended for questions/problems with specific technical solutions.
That said, here’s my view on this topic – I personally like to keep my
view functionsmall and if further processing is required, split them out into smaller functions.For example:-
FormMediaEquestContextis a class I import from another file and has its own logic which helps me to handle javascript and css files associated with my form (OrganizationAddUserForm).create_user_from_manualis yet another function which is encapsulated separately and deals with the reasonably convolutated logic relating to creating a new user in my system and sending an invitation email to that new user.And of course, I serve up a different template if this is the first time a user arrives on this “add user” page as opposed to redirecting to a completely different url with its own view function and template when the add user form is successfully executed.
By keeping our view functions reasonably small, I have an easier time tracking down bugs relating to specific functionality.
In addition, it is also a good way to “reuse” my utility functions such as the
create_user_from_manualmethod should I need this same utility function in another view function.However, at the end of the day, organizing code and encapsulating code is a judgement call that you get to make as you progress as a developer.