Build your first web application using Django

Python_logo_and_wordmark.svg

In our previous blog Introduction to Django, we discussed the Django’s features and architecture. In this blog, we will create a web application in Django.

For starting a new project, go to the folder where you want your project to be and run the command:

django-admin startproject django_proj

proj-struct
django-admin

Django’s command-line utility for administrative tasks.manage.py is automatically created in each Django project. manage.py does the same thing as django-admin but takes care of a few things for you:

  1. It puts your project’s package on sys.path.

  2. It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file. In our case, in manage.py file you can see the following:

os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘django_proj.settings’)

djando-admin

  1. Run django-admin help to display usage information and a list of the commands provided by each application.

  2. Run django-admin version to display the current Django version.

Apart from manage.py, you can see the django_proj folder, which is the name which we used for creating a project. Inside this folder, there is a file __init.py__ which is empty it just tells python that this is a python package.

setting.py-is used for all kinds of configurations for example database.

urls.py- is used for mapping URL to views. Right now, you will see :

urlpatterns = [
path(‘admin/’, admin.site.urls),
]

We will discuss it later when we run the application.

wsgi.py-Django’s primary deployment platform is WSGI (Web Server Gateway Interface). It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request.

Django’s startproject management command sets up a simple default WSGI configuration for you.

 Run application

python3 manage.py runserver

It will start your application at http://127.0.0.1:8000/

Screenshot from 2019-03-09 13-21-42

You can see the unapplied migrations warning on the terminal:

To access your Admin Interface, you need to initiate the database.

Initiate Database

python3 manage.py migrate

It will create necessary tables or collections depending on your DB type(default is SQLite), necessary for the admin interface to run.

Create Superuser

python3 manage.py createsuperuser

Then start your application again, as we saw in urls.py, where admin path is stated.

Go to http://127.0.0.1:8000/admin

Screenshot from 2019-03-09 13-28-48

Screenshot from 2019-03-09 13-30-29

Thanks for reading!

Knoldus-blog-footer-image

Written by 

Jyoti Sachdeva is a software consultant with more than 6 months of experience. She likes to keep up with the trending technologies. She is familiar with languages such as C,C++,Java,Scala and is currentky working on akka,akka http and scala. Her hobbies include watching tv series and movies, reading novels and dancing.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!