django first application

Let's start our Django application from the beginning. To start our first application we have to follow the below steps. Before going to start our project learn about django project layout.

  1. Create a directory with your project name & change your directory to the project. Lets consider our project as "Library Management"
  2. mkdir Library && cd Library
  3. Now, create virtual environment for our project.
  4. virtualenv env
    source env/bin/activate
    virtual environment is now ready to use.
  5. Install Django package in environment.
  6. pip install django
  7. Start our project Library Management
  8. django-admin startproject library_management 
    After executing the above commands a new folder "library_management" will be created. Inside that folder you can see another folder "library_management" and a file "manage.py". Inside "library_management/library_management" folder you can find below files 
           __init__.py      settings.py       urls.py        wsgi.py
  9. Run the below command to test our project setup.
  10. python manage.py runserver
    Open your browser and hit the url You can see the below output.
    We have successfully setup our first django project.
    If you do not see the above output, you might have missed something. so, delete everything that you did and start from the beginning again to save our time. As we are beginners we don't know much to resolve errors.
  11. Now create an application "library" to show "hello world" as response in browser
  12. python manage.py startapp library
    After executing the above command in the directory you can observe that a folder with name "library" have created and a file "db.sqlite3" also. If you look inside of folder "library" you can find the following files.
    admin.py   apps.py   __init__.py   migrations   models.py    tests.py   views.py
  13. "db.sqlite3" is file database developed in python. It is best suitable for beginners. we can also use MySQL and Postgres databases. "library" is our application with its files in it.
  14. Now, open "library_management/urls.py" and replace the contents of file with below code.
  15. from django.conf.urls import url
    from django.contrib import admin
    from library import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^hello-world/$', views.hello_world),
  16. And open "library/views.py" file and replace file contents with below code
  17. from django.http import HttpResponse
    def hello_world(request):
        html_source_code = """
            <title> Hello World</title>
                    <h1> Hello World</h1>
        return HttpResponse(html_source_code) 
  18. Open your web-browser and access url "". You can find "Hello World" as response.
Now we have successfully created "hello world" application. In the next post I will explain project, application and modules inside of it. Because it will become more lengthy if I explain it here.

Buy a product to Support me