Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating Django App Views

Tags:

django

views

If the app/views.py file gets very large, should I separate it? If so, what is the best way to do this?

like image 297
Matt Avatar asked Mar 30 '09 07:03

Matt


People also ask

How do you split views in Django?

Splitting the Views view_a . Remove the views.py file and create a directory named views. Add a __init__.py file inside it and create the separated view files. This is an important step: import all the modules inside each view file.

Why are Django views called views?

The view describes which data you see, not how you see it. It's a subtle distinction. So, in our case, a “view” is the Python callback function for a particular URL, because that callback function describes which data is presented.

How will you import views in Django?

from django. shortcuts import render from django. http import HttpResponse def index(request): return HttpResponse("Hello World!") (Of course you can give any url to your my_new_app in your project's urls.py file.)


4 Answers

Some developers make their views a python package instead of a module. This simply means making a directory called views in your application then placing each view in its own module (file) in that package.

Then you create an __init__.py file (which is what makes it a package). This file can be empty or it can be import all the view modules into its own namespace.

If it is empty you would have to import each view you need directly otherwise you can import it just as though it were a views.py module.

like image 127
Van Gale Avatar answered Oct 13 '22 01:10

Van Gale


There is no generic best way. But there is a right way for your situation.

  • put the views in their own files and import them in your view. This is good just to see how it works
  • make a separate app inside the project to maintain a set of views
  • create your own generic views which share the views common to most of your apps

Just as an starting example: I recommend you to start from the model and work yourself up:

  • how many models do you have?
  • Do they actually all relate or can they be grouped?
  • if the can be grouped split the app into two apps
  • so you'll also split the views
  • determine which view functions are similar and make them generic.
like image 25
Andre Bossard Avatar answered Oct 12 '22 23:10

Andre Bossard


I'd separate out views with similar purpose or functionality into one file, and include that in views.py. I only do this for readability and maintenance. For instance, CRUD views for a particular object or group of objects.

By importing these views directly into the main views.py file, it allows people not familiar with your convention to find what's where.

views/object_view.py
like image 20
Josh Smeaton Avatar answered Oct 13 '22 00:10

Josh Smeaton


In an ideal world, you shouldn't have to do this. Instead, try to refactor your code into different django apps for each sub-purpose that your project needs. That way, you can partition your project even better than you could have if you only split the views.py file.

For tips on how to split up your project into different apps, I recommend reading James Bennett's Practical Django Projects, which is what I'm re-reading right now :)

like image 38
winsmith Avatar answered Oct 13 '22 00:10

winsmith