Hat
Hat

Reputation: 1731

split views.py into multiple files

I am having some trouble splitting my views.py file into multiple files in a views folder. I tried the methods from this question, but I keep getting an error message.

file structure:

users/
   __init__.py
   urls.py
   views/
      __init__.py
      sign_in_out_up.py

urls.py:

from users import views as user

urlpatterns = patterns('',
    url(r'^signup/', user.sign_in_out_up.signup),
    url(r'^signin/', user.sign_in_out_up.signin),
    url(r'^signout/', user.sign_in_out_up.signout),
)

When I try the above code, I get an error saying 'module' object has no attribute 'sign_in_out_up'.

I have tried users.views.sign_in_out_up.signup in urls.py, but then the error changes to name 'users' is not defined, which seems to be a step backwards.

I have also tried putting from sign_in_out_up import * in views/__init__.py

Has anyone else had problems with this or have any suggestions?

Upvotes: 1

Views: 4244

Answers (2)

tereshkd
tereshkd

Reputation: 21

Use

from .sign_in_out_up import *

in views/__init__.py instead.

Add '.' before 'sign_in_out_up'.

Upvotes: 1

Simeon Visser
Simeon Visser

Reputation: 122376

Write your code in urls.py as normal (i.e., import the view from that views module as if it were views.py) and add to __init__.py within views:

from sign_in_out_up import *

This ensures you can continue to split up views in the views module without having to update urls.py all the time.

Upvotes: 7

Related Questions