Saturday, March 19, 2016

Testing The Django Framework - Documenting The Process

I have decided to go through the installation of the Django framework on my Windows machine. I am doing the same thing simultaneously on my Linux machine, but am currently only documenting the process on Windows. There are many reasons for this, mainly because doing anything with Python on a Windows machine is categorically more difficult than doing just about anything on a Linux machine. In other words, if you are running Linux, you don't need a tutorial on installing and working with the Django framework. (More or less).

Basically, I just opened up a terminal/console and typed in:

$ pip install Django==1.9.4

Then, for good measure, I typed the following, to make sure everything is working:

$ python -c "import django; print(django.get_version())"

This is all easy to find information on the Django project website. I'm just following the basic tutorials. The real reason I am doing this is not just to show how to install Django. It has more to do with the fact that I want to look at the way Django sets up projects.

In essence, I want to look at the directory structure and basic files of a "skinned down", "bare-bones" Django project. I plan to begin developing apps in Django, and will be doing it on Github, in Github repositories. I really want to get down to the fundamentals of the structure of a Django project. I decided to do this by documenting the entire process from start to finish. So this may just be the first post in a series of posts on the topic.

Now that Django is installed, I go onto the next command. First, I want to make sure that I start my first Django project in the right directory. It doesn't really matter where I stick the project, but I have a special folder called "Refcards" where I am doing most of my development. So I will "cd" into that directory, to create the Django project I will be starting following the tutorial (all on the Django project website, accessible to anyone with a web browser and an Internet connection).

Once I am in the proper directory, I type this in the console:

$ django-admin startproject mysite

This creates/generates the basic "scaffolding" for my Django app.

In essence, this is what my last command generated, this directory structure with these files:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

We will stop there for now and come back to this directory structure and these files, to see what they all mean.

I will quote directly from the Django project website, where I found this basic tutorial to build your first Django app:

These files are:

The outer mysite/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.

manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.

The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).

mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docs if you’re a Python beginner.)

mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.

mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.

mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

We will come back to this in a following post. That's it for now. The main idea, for now, is the directory structure and files that Django created for us, what I called the "scaffolding", namely:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Next we will look at these files and see what they are all about.

No comments:

Post a Comment