Starting with Django: The Project files

So in the last article we looked at how to start a Django project.

This week I want to pay a little bit of attention to some of the files that created and how they are used. These files create the basis of the entire Django site and no matter how many apps you create, you will find yourself coming back to these basic files for some fundamental changes.

So a basic project will start with the following structure:

<Project>
    manage.py
    <Project>
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

As you can see, it creates a base folder with the name of your folder. There are 2 items in here, manage.py and a folder also named as your project. Now I move the contents of this folder to the root folder, mostly because the first tutorials I followed also did this. I cant with any honesty or sincerity tell you why i would really do that other than I just find it easier.

So my base folder would look this like:
<Project>
    __init__.py
    manage.py
    settings.py
    urls.py
    asgi.py
    wsgi.py

Im sure you can agree it seems neater.

I wont really be talking about the __init__, asgi or wsgi files. These are mainly around the initiation and the web components (asgi and wsgi) but I don't understand enough of them or have any experience in modifying them.

Instead I will be focusing on the other 3 files (manage.py, settings.py and urls.py), as these are the main drivers located here.

manage.py: This file runs the core scripts that you interact with once the project has been created. You will use it from the cli to create new apps, you will use it on the cli to access the django shell, you will use it on the cli to run tests. You wont edit it though (at least I never did)


settings.py: This is kinda like the blueprint to your project. In here you will add any apps you create, define where static folders are located, add a media folder if you want/need one, define where your database is and its credentials. I wont be going into any specifics at this stage as I'm not showing you how to create an actual project.

urls.py: This is like the index of your project. The URLs are defined here as a base level. So you would use this particular urls.py to redirect everything to your apps, which will also have their own urls.py files.

It is important that every time you add an app to your project that all three of these files are used. Start with manage.py to create your app, add the app to settings.py to ensure the project knows about it and then put it into the urls.py so that it gets redirected to the right place.

Now I'm sure any non python users are starting to think "what's with all the py files, where is the html, CSS and JavaScript?".

That comes in good time, though to be fair my personal site (fuzzygwalchmei.herokuapp.com has bootstrap CSS and JavaScript, a minimal CSS file for some personalisation that i may still get rid of, and some template html files that have python embedded. These are mostly driven out of the apps though.

ok, that's enough for this week, I will back next week where we start looking at our first app.

Comments

Popular Posts