Getting started with django with python3 and ubuntu

Tag line for django says “The web framework for perfectionists with deadlines.” and most would agree. django is rediculously fast to implement, its fully loaded with lots of utilitis, its secure and at the same time its highly scalable. some of the most popular sites like instagram, pinterest are built with django.

Let us get our hands dirty by getting started with django instead of talking about django features.

Which django version should I use ?

Here are the details about djnago release. you can choose latest version, however, I will stick with LTS version

Which Python version should I use ?

This is the most common question and following table with clarify your query

Django version Python versions
  1.11 2.7, 3.4, 3.5, 3.6
  2.0 3.4, 3.5, 3.6, 3.7
  2.1,2.2 3.5, 3.6, 3.7

In this tutorial we are going to use python version 3.6 (3.6.4 to be very specifuc) and latest django version 3.0.1

Installation

If you want to install specific version use following

$ pip3 install Django==3.0.1

Alternative way to install Django is as below

$ sudo python3 -m pip install django
Collecting django  
  Downloading Django-X.X.X-py3-none-any.whl (7.1MB)
    100% |████████████████████████████████| 7.1MB 186kB/s 
Requirement already satisfied: pytz in /usr/local/lib/python3.6/site-packages (from django)  
Installing collected packages: django  
Successfully installed django-X.X.X  

Checking the installed version

Simple way to check is using below command

$python3 -m django --version
3.0.1

another way

~$ python3
Python 3.6.4 (default, Jan 13 2018, 12:02:51)  
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.  
>>> import django
>>> print django.get_version()
  File "<stdin>", line 1
    print django.get_version()
               ^
SyntaxError: invalid syntax  
>>> print(django.get_version())
X.X.X  

Creating django project

Project is created using following command

$django-admin startproject mysite

This will create folder mysite having required files. Now let us start the application by running server.

$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 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.

December 28, 2019 - 07:55:49
Django version 3.0.1, using settings 'mysite01.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.  

Now your website is up and running

By default server status at port 8000. You can change port using following command

$python3.6 manage.py runserver 8080

Creating an app

The term application describes a Python package that provides some set of features. Applications may be reused in various projects. The term project describes a Django web application. The project Python package is defined primarily by a settings module, but it usually contains other things.Applications include some combination of models, views, templates, template tags, static files, URLs, middleware, etc. They’re generally wired into projects with the INSTALLED_APPS setting and optionally with other mechanisms such as URLconfs, the MIDDLEWARE setting, or template inheritance.

$ python3 manage.py startapp polls

This will create a sub-directory as below

├── mysite01
│   ├── db.sqlite3
│   ├── manage.py
│   ├── mysite01
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   ├── settings.cpython-36.pyc
│   │   │   ├── urls.cpython-36.pyc
│   │   │   └── wsgi.cpython-36.pyc
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── polls
│       ├── admin.py
│       ├── apps.py
│       ├── __init__.py
│       ├── migrations
│       │   └── __init__.py
│       ├── models.py
│       ├── tests.py
│       └── views.py

Now to have our very basic application working, let us create urls.py file under folder polls and add below code

from django.urls import path
from . import views

app_name = 'polls'
urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    path('first/', views.first, name='first'),
]

Please note “/’ after “first” url name.

We have updated url structure now let us code the view to be displayed if these urls are accessed. Update following code in polls/views.py

from django.shortcuts import render
from django.http import HttpResponse

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

def first(request):
    context = "Test Data"
    return render(request, 'polls/first.html', {'context': context})

As you see, we are displaying very basic message. This will give you basic idea about hor urls and views work. Now before you run the server again we need to inform our project about the application ‘polls’ by updating settings.py file. Update following in settings.py


INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Also update main project urls.py to include polls/urls.py. any url starting with domain-name+polls will be served from ‘polls/urls.py’


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

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

Now let us run the server again

$ python3 manage.py runserver

Now you can access this website at below path
http://127.0.0.1:8000/polls/

http://127.0.0.1:8000/polls/first/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.