DEV Community

Cover image for Django for Newbies
Desmond
Desmond

Posted on

Django for Newbies

Brief Introduction to Django

Django is a framework based on python used to build web applications. It was developed in a news agency and designed to make commom Web-devlopment tasks fast and easy..

  1. Install Python
    To get started you need to get python installed on your local machine.
    Python

  2. Download django
    To install django we first need to install virtualenv which helps prevent package version conflicts, thus with virtualenv you can have say Django 2.2 and Django 3.0 installed on one pc without version conflicts.
    After successfully installing virtualenv ,create a new directory for your project .I usually name mine **src..So mkdir src and cd src.Next run virtualenv env.
    This creates an isolated environment for all your project dependencies called **env(You can give it any name you prefer).To activate the virtual environment cd env and .\Scripts\activate..
    We finally go back to our project root cd.. and pip install django ,this will install the latest stable version of django.

3.Start your project
We can now start our project with django-admin startproject hello.(hello is the name of the project). Next cd hello and python manage.py runserver

Your dev web server should be running on 127.0.0.1:8000

Successfully Installed Django

Models

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.Django uses an object relational mapper (ORM) which converts you model classes into sql statements when you make migrations.

Example:
from django.db import models

class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)

Views

A view function, or view for short, is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no “magic,” so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py, placed in your project or application directory.

Sample view function
from django.http import HttpResponse
import datetime

def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)

Templates

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

Feel free to ask any questions and watch out for my upcoming series where we build a complete web app with an Api to serve our frontend..

Top comments (0)