[Answered ]-Django unable to split views

0đź‘Ť

âś…

I figured it out after some time and found out that the fault is between my keyboard and chair :-). The problem is not with the urls.py in my transactions app, but in the “main” app django creates for you where you import the urls.py from other apps. I have left there one url for transactions.views.entries and forgot it there, so it was complaining for this file and I haven’t seen it.

For anyone having similar issues – check yout main urls.py too!

Thanks everyone for their answers and comments.

👤j0hny

2đź‘Ť

There is nothing like "one views.py file per Django app". The startapp command simply creates a views.py file for you, just for the sake of putting the code somewhere. A view function can live anywhere as long as it is on your Python path. What this means is that it has to be importable so as the comments suggest you might be missing __init__.py in your views folder.

Writing views

A view function, or view for short, is simply a Python function that
takes a Web request and returns a Web response. This response can be
the HTML contents of a Web page, or a redirect, or a 404 error, or an
XML document, or an image . . . or anything, really. The view itself
contains whatever arbitrary logic is necessary to return that
response. This code can live anywhere you want, as long as it’s on
your Python path. There’s no other requirement–no “magic,” so to
speak. For the sake of putting the code somewhere, the convention is
to put views in a file called views.py, placed in your project or
application directory.

👤zxzak

0đź‘Ť

Instead of importing the views into the urls.py, you could try something like this.

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^entries$', 'transactions.views.entries.entries', name='entries'),
    url(r'^delete_entry(?:/(?P<id>[0-9]+)/)?$', 'transactions.views.entries.delete_entry', name='delete_entry'),

    url(r'^categories$', 'transactions.views.categories.categories', name='categories'),
    url(r'^delete_category(?:/(?P<id>[0-9]+)/)?$', 'transactions.views.categories.delete_category', name='delete_category'),
)
👤JRajan

Leave a comment