We are now familiar with many frameworks for web development such as , HTML , Django etc , Now we will learn , to build Web Application using Django . For creating a web application, first let’s create a project. To create a project, just enter into a directory where you would like to share your code, then run the following command ” django-admin startproject myproject

django-admin startproject myproject
Web Application in Django

To Create Project use

Web Application in Django

Or simply open your cmd and move to the directory by using command ” cd desktop ” or ” cd yourdirectoryname ” then you can create a folder using command ” mkdir djangoproject ” Inside this use the command : django-admin startproject myproject

Once your project has been created, you will find a list of files inside the project directory.

Let’s discuss each one of them .

manage.py – It is a command-line utility that lets you interact with this Django project in various ways. 

myproject/ – It is the actual Python package for your project. It is used to import anything, say –  myproject.urls. 

ProjectDirectory - Django tutorial - Edureka

init.py – Init just tells the python that this is to be treated like a python package.

settings.py – This file manages all the settings of your project. 

urls.py – This is the main controller which maps it to your website.

wsgi.py – It serves as an entry point for WSGI compatible web servers.

Note that to create your application, make sure you are in the same directory as manage.py and then type the below command:

1python manage.py startapp webapp

Now if we look at the ‘webapp’ directory, we have some extra things from the original myproject. It includes model, test which are related to your backend databases.

Next in Django tutorial, you need to import your application manually inside your project settings. For that, open your myproject/settings.py and add your app manually:

123456789INSTALLED_APPS = ('webapp','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',)

Once you have installed your app, let’s create a view now. Open your webapp/views.py and put the below code in it:

12345from django.shortcuts import renderfrom django.http import HttpResponse def index(request):return  HttpResponse("<H2>HEY! Welcome to digitechbits! </H2>")

In the above code, I have created a view which returns httpResponse. Now we need to map this view to a URL. We need a URLconf in our application. So let’s create a new python file “urls.py” inside our webapp. In webapp/urls.py include the following code:

12345from django.conf.urls import urlfrom . import viewsurlpatterns = [url(r'^$', views.index, name='index'),]

In the above code, I have referenced a view which will return index (defined in views.py file). The url pattern is in regular expression format where ^ stands for beginning of the string and $ stands for the end.

The next step is to point the root URLconf at the webapp.urls module. Open your myproject/urls.py file and write the below code:

1234567from django.conf.urls import include, urlfrom django.contrib import admin urlpatterns = [url(r'^admin/', include(admin.site.urls)),url(r'^webapp/', include('webapp.urls')),]

In the above code, I have added my webapp and included the webapp.urls. Now don’t forget to import django.conf.urls.include and insert an include() in the urlpatterns list. The include() function allows referencing other URLconfs. 

Note that the regular expression doesn’t have a ‘$’ but rather a trailing slash, this means whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to include URLconf for further processing.

We are now done with the coding part! Let’s now start the server and see what happens. To start the server, type the below command:

Course Curriculum

Python Django Training and Certification

Weekday / Weekend BatchesSee Batch Details

1python manage.py runserver

Hurray! We have successfully created a basic Web App.

Is Django worth learning in 2020?

  • Django is definitely worth learning, especially if you don’t have an experience with programming. It’s one of the most popular Python frameworks.
  • As far as the other advantages of Django, the framework offers many essential tools for building a regular app.

Is Django good for beginners ?

Django is a web-framework that can handle all of your data and handle it better than any web framework out there. Django is the 1st Web Framework for Python for a reason: it’s easy enough for the beginners and yet powerful enough for the pros.

I hope you have enjoyed reading this Django tutorial. We have covered all the fundamentals of Django, so you can start practicing now. After this Django tutorial, I will be coming up with more blogs on Django for web app development, REST framework, Django models and many more. Stay tuned for my next blog in this series!

Got a question for us? Please mention it in the comments section of this “Django tutorial” blog  and we will get back to you as soon as possible.

One Reply to “Build Your First Web Application in Django:”

Leave a Reply

Your email address will not be published.