Django is a scalable web framework that deals with web applications. It's easy to manage, easy to scale and easy to work with!

As a newbie, you might get overwhelmed by looking at the tutorials that span across a huge amount of information. This is an attempt to strip down Django to its barebones necessity and to get a project up and running, quickly!

Installation

Easy. Run pip install django to install with PyPI. (You can maybe install inside a virtual environment to skip conflicts in the global python space.)

Create a project

Run django-admin startproject mysite from inside your venv shell.

Yup.

This dude will auto-generate some basic code for you, along with a lot of files. Here's a structure -

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

manage.py is the manager (not even joking). So you can use

manage.py <something> <something>

OR django-admin <something> <something>

OR python -m django <something> <something> - they're all the same stuff.

Inside mysite/ (the one with __init__) , you got some files (this is your website folder). The mysite at the top is just like a wrapper around a gift, you can change it('s name) anytime you want.

To keep it simple, let's ignore all other files for now and focus on the urls.py. This little dude will contain the paths to your various webpages inside your website.

Apps

Now, Django works with apps. Everything you'll see is basically a web application (it can be simple polling app, can be just some text on display, can be a database system, anything). And the best thing is they're pluggable (so you can use the same app in different websites).

A website contains apps along with some other configurations.

Your apps can live anywhere on your Python path. In this tutorial, we’ll create our poll app in the same directory as your manage.py file so that it can be imported as its own top-level module, rather than a submodule of mysite.

To create your app, make sure you’re in the same directory as manage.py and type this command:

python manage.py startapp polls

That’ll create a directory polls, which is laid out like this:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

Views

It's basically the webpage that you're supposed to view. On requesting a particular URL.

You might've guessed it by now. Yup, Open the file polls/views.py and put the following Python code in it:

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

This is the simplest view possible in Django. To call the view, we need to map it to a URL - and for this we need a URLconf.

To create a URLconf in the polls directory, create a file called urls.py. Your app directory should now look like:

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    urls.py
    views.py

In the polls/urls.py file include the following code:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

You just linked your view to a URL inside your app.

The next step is to include your app's URL(s, since there might be multiple views in your app) in your main website.

In mysite/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

The include() function allows referencing other URLconfs. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/urls.py), they can be placed under “/polls/”, or under “/fun_polls/”, or under “/content/polls/”, or any other path root, and the app will still work.

You have now wired an index view into the URLconf. Verify it’s working with the following command:

django-admin runserver

Go to http://localhost:8000/polls/ in your browser, and you should see the text “Hello, world. You’re at the polls index.”, which you defined in the index view.

Path

The path function contains 4 arguments - route, view, kwargs and name.

We ignore kwargs for now. Here is an example with other three.

path('/helloworld', views.helloWorld, name='hello-world')

/helloworld is your URL route for the method helloWorld defined in the views of this app. The name is just a unique name for this particular route so that you can access it from any other part of your code.

That's it. You now know about the basic request and response flow in Django!