- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Build your own website using Django in Python
Django is a Python web framework that is both free and open source.
Why Use Django?
It’s very fast.
Comes with a lot of pre-existing features like user authentication, site maps, RSS feeds.
It is very secure and prevents a lot of security mistakes like SQL Injection, cross−site scripting, clickjacking etc.
It is very scalable and thus can be used even when the network traffic is exceedingly high.
Now that you know why we would be using Django to build our web application. Let us start setting up the ground work for it.
Setting up an environment
While building our web application, we will be using various packages that we wouldn’t require outside of our workspace for the website. In order to ensure we use a dedicated space for the website, we create a virtual environment for it.
To do this, we will be using the virtualenv package. Let us install it first,
Python −m pip install virtualenv
Now, create a folder for our website say Django-intro. Once the folder is created, it’s time to set up the virtual environment inside it. To do so, launch your terminal and `cd` your way to the project directory and use the command
This should create a folder with the name env. In order to enter this virtual environment, you’ll have to use the command
If you have the name of your environment with brackets around it, then you have successfully entered the virtual environment.
Firstly, make sure you have Python installed, version 3.6 or above.
Next up, install Django using Pip.
Python −m pip install Django
Verifying your Django installation.
python −m Django version
And, that’s it! You are now done with the initial phase of getting your website running. Let us now jump into creating our first application for the website!
Creating an application
Let us now obtain the skeleton required to get started. To do this, run the following command when inside the virtual environment.
django−admin startproject Django−intro−app
This should create a basic skeleton for the Django app.
If you enter the Django−intro−app folder, you can see that it has a manage.py file and another Directory with the same name Django−intro−app and settings.py, urls.py, and wsgi.py files.
Points to Note
Settings.py contains all the various settings for your project.
Urls.py contains all the different routes for the website.
Application we built is called Django−intro−app.
Every subsequent app we build for the website will be within the main folder and have its own name.
Now that you understand the main framework of the entire Django workspace, let us create an application.
First, `cd` to the project directory −> Django−intro−app.
Python manage.py startapp first−app
This should now create a directory called first-app which contains admin.py, apps.py, models.py, tests.py and views.py files within the folder.
Next up we need to make sure the first-app application is recognised by Django. To do this, head over to Django−intro−app/settings.py and add first−app into the INSTALLED_APPS section.
This should now look like,
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'howdy' ]
And, that’s it! You have now created a Django application. In order to test out our web−application, let us run it using Django’s web server.
To do this, head to the project directory and use the command
Python manage.py runserver
If you have done everything like mentioned above, your website should be running at http://127.0.0.1:8000/. Open the link and checkout your very own Django webpage.
You have now learnt to set up a virtual environment to work your Python projects and Django applications.
You have created your very own Django application. Obviously, this is not the end. You can create your very own templates for the website using Html, CSS and JS. Modify URLS, link various pages and do a lot more with Django!
For more details and information about the Django series check out their very own documentation at https://www.djangoproject.com/.
- Build Your Own Botnet
- How to build your own Sqlite database in Python
- Making your own custom filter tags in Django
- Print with your own font using Python?
- How to implement django-material in your Django project?
- Django – Making a Django website more human-like using Humanizer
- How to add a captcha in a Django website?
- QR code generating website in Django
- How to Validate your Website Code?
- Print with your own font using C#
- Implement your own sizeof operator using C++
- Creating a screenshot taking website in Django
- How to create Favicon for your website?
- How to Prevent Your Website from Hackers?
- Your Website Needs More!