FinalsClub / djKarma

GNU General Public License v3.0
9 stars 1 forks source link

Use a standard Django project layout #176

Open LucianU opened 11 years ago

LucianU commented 11 years ago

I strongly recommend using a project layout that follows both Python and Django conventions. Thus, your project should look like this:

djKarma/
    djKarma/
        __init__.py
        manage.py
        urls.py
        wsgi.py
        settings/
            __init__.py
            local.py
            common.py
            development.py
            staging.py
            production.py
        apps/
            KNotes/
        static/
        templates/
    fabfile.py (optional)
    setup.py (optional)
    README
    requirements/
        common.txt
        development.txt
        production.txt
    bin/
        <scripts here>
    confs/
         development/
         staging/
         production/
    docs/
    tests/

See this link for info regarding the standard Python project layout: http://guide.python-distribute.org/creation.html Now, about other specifics of this layout, I think it's better to have an apps directory for the Django apps. This way, you don't keep all the apps in the root of the Django project, and it's easier to make the apps standalone. By adding the apps directory to PYTHONPATH, you can do imports like this: from KNotes.models import Foo, instead of doing from djKarma.KNotes.models import Foo.

Another thing is that I prefer to keep templates in their own templates directory. This avoids being able to access the template files directly in the browser and seeing the template code which might give some useful information to an attacker.

sethwoodworth commented 11 years ago

I took the new layout suggested by django 1.4 when laying out this project and haven't really rethought it. We have a notes app and the KNotes folder contains the project code, settings.py &etc.

We had a templates directory in the KNotes project folder, but I'm putting our new design templates in the app that hosts the relevant application code. But as we only have one app, I don't see the benefit of that in this project layout. You suggest a single templates directory in the project folder over app specific templates?

LucianU commented 11 years ago

Indeed, the current project layout is similar to the default one created by django 1.4 startproject, except that the inner directory, KNotes, doesn't match the name of the outer directory, djKarma.

I see a Django project like this: everything that is inside the inner directory (second djKarma in my version, KNotes in your version), is the meat of the project, the Python code and the templates, client-side code and static resources. Everything out of this directory is meta information: requirements, configurations, setup code, scripts, etc. Here is how a popular Django boilerplate project organizes things: https://github.com/rdegges/django-skel

Another thing I notice is that here an app equals the entire site, which is not what a Django app is. A Django app offers some specific functionality. For example, you would have an accounts app handling signup, login (if you need to do custom stuff), password stuff; maybe a profiles app, if you have a lot of logic relating to profiles. I also see you have a Jobs page. That might expand into a separate app, if you want to offer more functionality for that section.

Regarding the templates, I think both approaches are desirable. Keeping app-specific templates in a templates directory inside the app goes along with the standalone thing. At the same time, you will use in the future an app whose templates you need to provide or override. For that, you should have the global templates directory. Also, the base template and the 404.html and 500.html templates should be in the global templates, because they're not specific to an app, but to your site.