Skip to content

Instantly share code, notes, and snippets.

@nahiyan
Last active January 26, 2019 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahiyan/187f27c8afbf422fbbdbb857e8d88193 to your computer and use it in GitHub Desktop.
Save nahiyan/187f27c8afbf422fbbdbb857e8d88193 to your computer and use it in GitHub Desktop.

Overview

  1. Our main goal is to use django to create a project.
  2. Since, django is a python framework, we need to install python to run it.
  3. However, there are 2 major versions of python available - python 2 and python 3.
  4. We're going to use python 3 since it's the latest version available and is way better than python 2.
  5. Pip is the package manager for python, we use pip to install python programs so we don't have to download, configure and install it ourselves.
  6. Managing python versions (2 and 3) is a bit tedious, so we're going to use virtualenv.
  7. Virtualenv will help us create isolated environment for our django project so we can set which python version we're going to use and not worry about anything at all. There are many other benefits of using it which you'll see later.

Installing python3

sudo apt-get install python3

Installing pip3

sudo apt-get install pip3

Installing django

Since django is a python framework, it's best to install it using pip:

pip3 install django

Installing django should automatically install the command line tool called "django-admin."

Create our project with django-admin

Next, we're going to use django-admin to create our django project:

django-admin startproject [project-name]

Enter your project by using cd:

cd [project-name]

Isolating our project with virtualenv

Virtual environment isolates our project. It helps us select a version of python and not worry about it afterwards.

virtualenv [name] -p /usr/bin/python3

Make sure you cd into your project before running the command.

Entering the virtual environment

source [name]/bin/activate

Running django's server

python manage.py runserver

Exiting the virtual environment

deactivate

Overview

  1. In your django project, you're going to have a main folder which will have the same name as your project name.
  2. In the main folder, you'll have a "urls.py" file. This file is responsible for defining the URLs of your site.
  3. Your django project will be divided into apps. Each part of your site will be an app.
  4. Whenever you want to show anything to the user, you'll show them using "views."
  5. Each app will have views. They'll be contained in "views.py" folder.

Urls

The main folder will have a "urls.py" file, which will contain URL definitions.

For example, Facebook has the following URLs:

  • facebook.com/
  • facebook.com/messages/
  • facebook.com/settings/

For each URL, we need to connect it to a view, because in our case, every URL visit will show something to the user.

Here's an example of what the python code for the URL patterns of Facebook may look like:

urlpatterns = [
    path('', views.index),
    path('messages/', views.messages),
    path('settings/', views.settings),
]

Apps

Your django project will be divided into apps. Each part of your site will be an app.

For example, if we talk about facebook:

  • The news feed is an app
  • The messages section is an app
  • The settings is an app

You can create a new app using:

python manage.py startapp [app-name]

Whenever, you create an app, make sure you add it to the list of installed apps in "settings.py" file of your main folder.

Views

Whenever, you want to show something, maybe a page, you need to create something called a view.

Each app will have a "views.py" file.

In the "views.py" file, you'll define a view using python functions.

Here's an example of a python function:

def index(request):
  return render(request, "pages/index.html")

The function has the name "index," and takes an argument of "request" and returns something.

Templates

Whenever you want to output an HTML page to the user through a view, you need to create a template.

Let's say you want to create a template file called "index.html" in an app called "pages."

To do so, we need to create a folder in your app called "templates." Next, you need to create another folder name as your app's name, "page." Then, place the "index.html" or whatever template file[s] you want.

So the final address to your "index.html" file may look like this in your django project: pages/templates/pages/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment