Skip to content

Latest commit

 

History

History
179 lines (151 loc) · 6.07 KB

File metadata and controls

179 lines (151 loc) · 6.07 KB

Django - Hello World

Home | ← Prev | Next →

In this session we're going to dive into the django framework and create our very first project using django.

Here we'll try to create a TodoApp. I hope by now you're pretty clear about the basics and the concepts of web, wsgi and some pythonic tools we've discussed in our previous sessions so far, if not then you might want to check these links.

TodoApp

Now let's start building our app.

Setup virtualenv

Let's start by setting up virtualenv with Python 3+.

We'll create a virtualenv named py3 under envs directory.

 ➜ mkdir ~/envs
 ➜ virtualenv -p /usr/local/bin/python3 ~/envs/py3

Now activate it.

source ~/envs/py3/bin/activate

You should now be able to see a virtualenv identifier py3 near your prompt in the terminal like this:

(py3) 
[kabir@leapfrog] ~

Install django

Now it's time to install django. Install it using pip:

 ➜ pip install django

Test your installation

 ➜ django-admin --version

Create a project

Now, let's go and create our very first project.

 ➜ django-admin startproject todoapp

You should be able to see that todoapp directory has been created with some boilerplate code already generated by django-admin. Open it using your favorite code editor and go through the code.

Folder structure should look similar to this:

todoapp
├─todoapp
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

Create new todos app

Now we need to create a new django app todos. We can do this using:

cd todoapp
 ➜ python manage.py startapp todos

Check the code again, you can see new directory todos has been created with some more code. Go through them too.

Now the new directory structure would look like:

todoapp
├── todoapp
│        ├── __init__.py
│        ├── settings.py
│        ├── urls.py
│        └── wsgi.py
├── todos
│        ├── __init__.py
│        ├── admin.py
│        ├── apps.py
│        ├── migrations
│        ├── models.py
│        ├── tests.py
│        └── views.py
└── manage.py

You now need to register this new app todos into our todoapp project. Open todoapp/settings.py and append todos to INSTALLED_APPS list:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'todos'
]

Run the server

Let's run the development server now to test our very first app.

 ➜ python manage.py runserver

You would see server has started now.

 ➜ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 28, 2017 - 21:56:40
Django version 1.11, using settings 'todoapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[28/Apr/2017 21:56:54] "GET / HTTP/1.1" 200 1716
Not Found: /favicon.ico
[28/Apr/2017 21:56:54] "GET /favicon.ico HTTP/1.1" 404 1963
[28/Apr/2017 22:00:31] "GET / HTTP/1.1" 200 1716

Go to http://127.0.0.1:8000/ you should be able to see our app is up and running.

But you may see some warnings shown on the page. This is because we still haven't run the database migrations yet.

Migrations

Let's run the database migrations now. First you need to press Ctrl + C to end the server in your terminal.

Then you can run:

 ➜ python manage.py migrate

You should be able to see the migrations running.

 ➜ python manage.py migrate
Operations to perform:
    Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
    Applying contenttypes.0001_initial... OK
    Applying auth.0001_initial... OK
    Applying admin.0001_initial... OK
    Applying admin.0002_logentry_remove_auto_add... OK
    Applying contenttypes.0002_remove_content_type_name... OK
    Applying auth.0002_alter_permission_name_max_length... OK
    Applying auth.0003_alter_user_email_max_length... OK
    Applying auth.0004_alter_user_username_opts... OK
    Applying auth.0005_alter_user_last_login_null... OK
    Applying auth.0006_require_contenttypes_0002... OK
    Applying auth.0007_alter_validators_add_error_messages... OK
    Applying auth.0008_alter_user_username_max_length... OK
    Applying sessions.0001_initial... OK

Now re-run our server using:

 ➜ python manage.py runserver

And go to http://127.0.0.1:8000/ again, you can see the warnings are gone by now.

Great. You've just created your first app with django. Now let's change some code. Check the full source code for the todoapp here.

Exercises

  1. Create a bootstrap templates for the following pages:
    • Todo list
    • Add todo form
    • Edit todo form

Next Step

In the next tutorial we'll be working more with the templates and views to complete the UI of our application.