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
To Create Project use
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.
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:
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:
Once you have installed your app, let’s create a view now. Open your webapp/views.py and put the below code in it:
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:
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:
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:
Python Django Training and Certification
Weekday / Weekend BatchesSee Batch Details
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.