Get Started With Django

Published on May 04, 2020 | Bikesh Bade | 3395 Views

Why You Should Learn Django

 

There are endless web development frameworks out there, so why should you learn Django over any of the others? First of all, it’s written in Python, one of the most readable and beginner-friendly programming languages out there. The second reason you should learn Django is the scope of its features. If you need to build a website, you don’t need to rely on any external libraries or packages if you choose Django. This means that you don’t need to learn how to use anything else, and the syntax is seamless as you’re using only one framework.

 

Django is a high-level web application framework with loads of features. It’s great for anyone new to web development due to its fantastic documentation, and particularly if you’re also familiar with Python.Django apps are structured so that there is a separation of logic. It supports the Model-View-Controller Pattern, which is the architecture on which most web frameworks are built. The basic principle is that in each application there are three separate files that handle the three main pieces of logic separately:

 

  • Model defines the data structure. This is usually a database and is the base layer of an application.
  • View displays some or all of the data to the user with HTML and CSS.
  • Controller handles how the database and the view interact.

 

It’s time to install Django.and Next step is to create a Django project:

 

pip install Django

 

Next step is to create the first Django project

 

django-admin startproject firstproject

 

This will create a new directory firstproject. If you cd into this new directory, you’ll see another directory called firstproject and a file called manage.py. Your directory structure should look something like this:

 

 firstproject/
    ├── firstproject/
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    │
    └── manage.py


Once your file structure is set up, you can now start the server and check that your set up was successful. In the console, run the following command:

 

python manage.py runserver

Then, in your browser go to localhost:8000, and you should see the following:

 

 

Congratulations, you’ve created a Django site! The source code for this part of the tutorial can be found on GitHub. The next step is to create apps so that you can add views and functionality to your site.

 

Create a Django Application

 

To create the app, run the following command:

 

python manage.py startapp hello_world

 

This will create another directory called hello_world with several files:

 

  • __init__.py tells Python to treat the directory as a Python package.
  • admin.py contains settings for the Django admin pages.
  • apps.py contains settings for the application configuration.
  • models.py contains a series of classes that Django’s ORM converts to database tables.
  • tests.py contains test classes.
  • views.py contains functions and classes that handle what data is displayed in the HTML templates.

 

Once you’ve created the app, you need to install it in your project. In firstproject/settings.py, add the following line of code under INSTALLED_APPS:

 

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello_world',
]

 

That line of code means that your project now knows that the app you just created exists. The next step is to create a view so that you can display something to a user.

 

Create a View

 

Views in Django are a collection of functions or classes inside the views.py file in your app directory. Each function or class handles the logic that gets processed each time a different URL is visited.

 

Navigate to the views.py file in the hello_world directory. There’s already a line of code in there that imports render(). Add the following code:

 

from django.shortcuts import render

def hello_world(request):
    return render(request, 'hello_world.html', {})

 

In this piece of code, you’ve defined a view function called hello_world(). When this function is called, it will render an HTML file called hello_world.html. That file doesn’t exist yet, but we’ll create it soon.

The view function takes one argument, request. This object is an HttpRequestObject that is created whenever a page is loaded. It contains information about the request, such as the method, which can take several values including GET and POST.

Now that you’ve created the view function, you need to create the HTML template to display to the user. render() looks for HTML templates inside a directory called templates inside your app directory. Create that directory and subsequently a file named hello_world.html inside it:

 

mkdir hello_world/templates/
touch hello_world/templates/hello_world.html

 

Add the following lines of HTML to your file:

 

<h1>Hello, World!</h1>

 

You’ve now created a function to handle your views and templates to display to the user. The final step is to hook up your URLs so that you can visit the page you’ve just created. Your project has a module called urls.py in which you need to include a URL configuration for the hello_world app. Inside firstproject/urls.py, add the following:

 

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

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

 

This looks for a module called urls.py inside the hello_world application and registers any URLs defined there. Whenever you visit the root path of your URL (localhost:8000), the hello_world application’s URLs will be registered. The hello_world.urls module doesn’t exist yet, so you’ll need to create it

 

Inside this module, we need to import the path object as well as our app’s views module. Then we want to create a list of URL patterns that correspond to the various view functions. At the moment, we have only created one view function, so we need only create one URL:

 

from django.urls import path
from hello_world import views

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

 

Now, when you restart the server and visit localhost:8000, you should be able to see the HTML template you created:

 

 

Congratulations, again! You’ve created your first Django app and hooked it up to your project.

Responses

Leave your comment