Understanding Django Workflow, structure, files and deep inside


Django is special framework that build base on Python. If you familiar with PHP Framework like CodeIgniter, CakePHP or another else, you will find Django is different. For futhermore explanation, I assume you using Ubuntu (I use 11.10) , know how to install Django and run through “python manage.py runserver”.

Back to why Django is different, actually it installed in OS as native python packages. That’s same way when you install another python modules packages through python-pip and all installed located in “/usr/local/lib/python2.7/dist-packages/”. After installed, when can execute some django command through console like “django-admin.py”.

When we start creating web with Django, then we should create a projects. Project in Django is a directory that contains all of the settings of an instance of Django. Creating projects in Django is easy by running “django-admin.py startproject mywebsite”. This command will create a directory called mywebsite and files inside :

1
2
3
4
5
mywebsite/
    __init__.py
    manage.py
    settings.py
    urls.py

You can run django by running command “python manage.py runserver” inside created project. Remember, project is a root of an Django instance.

As I told before, project contains all of the settings (settings.py) also with url route (urls.py). Mostly this two files are important and commonly modified in a Django projects.

Now we have a project directory. Then, we want to create website that have news section, blog section, maps section or another. We can create applications based on what we want inside a web. For instance, for creating news page on website, we create application called “news”. Or, we want a blog inside the web, so we create blog apps.

Create apps is easy by running “python manage.py startapp news”. It will create news application inside projects. When developing an apps, you should make it independently, less modify projects settings to load apps.

For example, in projects, there is files that manage url route called “urls.py”. We create “news” application and want it works when user open “http://domain/news”. You can define this route on urls.py in projects. But what happen if you move this “news” apps into another projects? You will open the first projects, copy setting into the new one. This is not good!

Django apps is create to easy our development and work like plug-and-play system. So, the right way is define all route (urls.py) inside “news” apps and load it on projects urls.py.

Now you should know basic development workflow in Django.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.