1

Hopefully this is a simple noob question. I'm working on my first large(ish) Django project. The first engineer on the project was following the default Django code layout. As things have grown we've recently split out models into their own directory with one file per model.

It's time to start doing the same for views. However, I don't want/need to do it all in one go. I'd like to just start moving things out of the default views.py one by one as I work on them. However, I'm having difficulty getting urls.py to work with both a views directory and views.py

Is this just going to cause a naming collision when I try to import 'views' in my urls.py file? Is the simple answer just to call "views" something else while I make the transition? Or just bite the bullet and do it all at once?

August Flanagan
  • 886
  • 14
  • 27

3 Answers3

2

I recommend creating feature sub-directories within your app folder on your Django project. Then, simply import them from the new directory using the app.views namespace by using the from ... import * incantation. This will import any views without the module name prepended (e.g. what would normally be referenced by app_name.views.feature_one.view_class becomes app_name.views.view_class like you want). See below:


# app_name/views.py
from feature_one.views import *
from feature_two.views import *

# ... copy views from here

# new file: app_name/feature_one/views.py

# ... paste some views here

# new file: app_name/feature_two/views.py

# ... paste some other views here

# new file: app_name/feature_one/__init__.py

# ... this file can be blank. required for importing "feature_one" like a module

# new file: app_name/feature_two/__init__.py

# ... this file can be blank. required for importing "feature_two" like a module

Now, while your views will be spread across sub-directories, they are all imported with the same names into app_name.views so you can still reference the same names in urls.py despite having moved some views to other files.

pztrick
  • 3,741
  • 30
  • 35
2

The direct answer is yes, you have a python naming collision. See for example Python Import Class With Same Name as Directory

You don't need to do it all as once though -- you can simply rename / move your views.py file or your new views directory. Moving the new directory would likely be easiest, then you won't have change your existing url routes.

There's nothing special about views.py files, as long as your urls.py points to the appropriate function, you can place them anywhere, and call them anything.

Community
  • 1
  • 1
Nils
  • 5,612
  • 4
  • 34
  • 37
0

Since you have divided models into their sub-apps, you can define urls specific to these sub-apps in the created sub directories and set up a url hierarchy. This is how your default urls.py will look like:

from django.conf.urls import include, patterns, url

urlpatterns = patterns('',
    # ... snip ...
    url(r'^comments/', include('your_website.comments.urls')),
    url(r'^community/', include('your_website.community.urls')),
    url(r'^contact/', include('your_website.contact.urls')),
    # ... snip ...
)

where comments, community and contact are your newly created sub-apps/sub-directories. More info on how the url dispatcher works, here

shaktimaan
  • 11,962
  • 2
  • 29
  • 33