Dave Partner

Web development with Django continued - SimpleBlogger Project part 1

  • 0 Replies
  • 358 Views

0 Members and 1 Guest are viewing this topic.

THE PROJECT
With Python and Django installed on your system, open your command prompt (for Windows) or bash console (for Mac),
navigate to the directory you want the project to reside using the cd command, then enter the following command.

   django-admin.py startproject <project_name>

in our example we use;
   
   django-admin.py startproject simplebloggerproject

Running this command creates a Django project with the name you specified in the directory you specified. Don't worry if you don't
see any change in your command prompt, it is normal. To see what has changed, go to the directory where you created the project (e.g desktop),  you should see a new folder with the name of the project you just  created.
This folder contains two items - a sub-directory with the same name and a python file with the name 'manage'.
The sub-directory contains four files namely;  __init__, settings, urls and wsgi - all ending with a .py(python's extension).

So far our folder structure looks like this

simplebloggerproject
   |
   |__simplebloggerproject
   |    |__   __init__.py
   |    |__   settings.py
   |    |__   urls.py
   |    |__   wsgi.py
   |
   |__ manage   

We will explain what each of these file does as we progress.

NB: you can download the sample project here >> https://github.com/EzechukwuJI/simplebloggerproject

THE APP
Django is designed to be as flexible and portable as possible, with the DRY (Don't Repeat Yourself) principle
at the core of its development processes.

The project created above is a container for any number of applications you want to create. Each of these
applications have an independent existence and are connected with urls. This makes it interestingly easy to create pluggable micro-components for for the entire project.

In our example, we will create our first application and we will simply call it "simple_blogger".

To create the app, open your command prompt and navigate to the simplebloggerproject project directory, then run the command below.

   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py startapp <app_name>

in our project we'll have

   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py startapp simple_blogger

This creates the simple_blogger App inside the simplebloggerproject  directory.

The App we just created is a directory with six items - a sub-directory with the name "migrations", __init__.py, admin.py,
models.py, tests.py and views.py.
Each of these files have their unique functions. We will add more file as we proceed.

WHAT WE HAVE COVERED SO FAR
If you followed correctly, you should have a project tree similar to the one below.

simplebloggerproject
   |
   |__simplebloggerproject
   |   |__   __init__.py
   |   |__   settings.py
   |   |__   urls.py
   |   |__   wsgi.py
   |
   |__ manage
   |
   |__ simple_blogger
      |__  migrations
      |      |__   __init__
      |
      |__  __init__
      |__  admin
      |__  models
      |__  tests
      |__  views

That's about all we have to do to get our first Django App up and running.

VIEWING THE APP (WEBSITE) ON THE BROWSER
Before we can view what we have so far, we have to complete a few setup.

Open the simplebloggerproject directory and open the settings.py file in any text editor of your choice.
On the settings file, locate the INSTALLED_APP tuple and add the name of the application we just created,

INSTALLED_APPS = (
   '',
   'simple_blogger',
   )
NOTE: Don't forget to add the trailing comma at the end of the App name as shown.

As we create more Apps, we add them to project in the same way. We will explain the other parts of the
settings.py as we add more functionalities to the project.

Next step is to initialize our project by migrating the changes we have made together with the default
settings and db models automatically created at the point of creating the project and the App.

Back to the command prompt and in the simplebloggerproject directory run the following command.
   
   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py migrate

After the migration has completed, we can then start our server by running this command

   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py runserver

After you've started your server, open your browser and on the address bar, enter the following
   
           127.0.0.1:8000

If you followed the steps correctly, you should see a congratulatory message.
By default, Django runs on port 8000. However, you can specify a different port at the point of running the server like this;
   
   manage.py runserver 9000

This will run the server on port 9000, and to access the App, 
enter 127.0.0.1:9000 in the browser's address bar.

Congratulations! you just created your first Django based website

NB: you can download the sample project here >> https://github.com/EzechukwuJI/simplebloggerproject

NEXT STEP
In our next tutorial, we will explain the different parts of the project and application we just created and add more functionalities.


If you have any questions you can drop it in the comment section or on the open Facebook group (link below) for quicker responses.

Join the Facebook group here >> https://www.facebook.com/groups/466737186852664/

Please signup on this site to get alerts as subsequent tutorials are published. Signup here => http://davepartner.com/register/

See you in the next tutorial.