Django development for beginners

Django for beginners

Django at a glance

  • Django is a python based open source free web framework
  • It follows the MTV (Model-Template-View) architectural pattern. It also appears to be a MVC framework, but you call the Controller the “view”, and the View the “template”
  • It is maintained by Django Software Foundation.
  • Its primary goal to ease the creation of complex, database-driven website or application.
  • It also provides an optional administrative CRUD operation interface that is dynamically generated through introspection and configured via admin models.

Django project and app

 Django project is a collection of one or more app that contains all the settings for an instance of Django.
This would include database configuration, Django specific options and application specific settings.
And each app is a python package such as we can imagine a big project like ERP and its apps are the modules of that ERP.
Every python project contains core or master app that provides a way to go/access to other apps.

How to Install Django

Ensure that latest version of python is installed. You can see version using the command

python --version

Ensure that latest version of pip (python package installer) is installed. You can see version using the command

pip –version

Execute command

pip install django

it will take few minutes and you can check its version using command

python -m django –-version

for more click here.

How to create Django project

Let us create a hello world type web application using Django. To that we will execute a command

django-admin startproject myblog

Here myblog is the project name. this command will create the code as below

myblog/
    manage.py
    myblog/
        __init__.py
        settings.py
        urls.py
        wsgi.py

 Where

  • Outer myblog/ directory: the container folder for the project. It doesn’t matter even if we change this directory name.
  • manage.py: A command-line utility to interact with this Django project in various ways. For more you may visit the link.
  • Inner myblog/ directory: The main python package for this project and its name will be used to access anything inside it such as myblog.urls
  • myblog/__init__.py: it indicates that this folder is a package folder.
  • myblog/settings.py: Settings/configuration file for this Django project. For more, read Django settings
  • myblog/urls.py: Routes configuration file for this Django project. For more, read Django routing
  • myblog/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. For more, read WSGI.

How to add python app in a project

If want to create app other than main app (myblog), we have to execute the command

Python manage.py startapp postsCode language: CSS (css)

here posts is the project name. this command will create the code as below

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

 Where

  • apps.py: python app configuration file. Read more for AppConfig class in django.apps package.
  • models.py: it contains model classes for database entities.
  • admin.py: Here models are registered to Django.contrib.admin.site
  • views.py: It contains controller methods for each route declared in urls.py

Run development server

Let’s verify the Django project works. We have to run the following commands:

python manage.py runserver 4300Code language: CSS (css)

Here 4300 is the port number. We will see the following output

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.
October 23, 2019 - 17:38:29
Django version 2.2.5, using settings 'pyproject.settings'
Starting development server at http://127.0.0.1:4300/
Quit the server with CTRL-BREAK.Code language: JavaScript (javascript)

I think it will help a developer for quick start. Please feel free to leave any comments or for any clarification or further help, I will try my best to reply as soon as possible. Also you can contact with iXora Solution Python Team for any help on your project implementation at Contact-iXoraPython

Add a Comment

Your email address will not be published. Required fields are marked *