In this post, we will create a Hello World web project using Django framework.

I assume that you have already installed Python and PyCharm. If not, click here to set up Python on Windows. Also, click here to understand Django directory structure.

Here are the steps:

  1. Open command prompt as administrator and execute the command:
    pip install django

    This will download and install Django on your system.

  2. Now start PyCharm. Create a new Project and select Django as the Project Type. Give it name: djhello. Set app as the application name.
  3. You will see djhello folder inside the project’s djhello folder. I find it a bit odd. Let us rename the inner djhello folder to project to avoid confusion.
  4. Open manage.py file in your project’s folder and modify the line:
    o.environ.setdefault("DJANGO_SETTINGS_MODULE", "djhello.settings")

    To

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

    This will tell Django environment to read settings from the project\settings.py file.

    manage.py
    is a command line utility that let us interact with our project. For example, to run our database migrations, we can execute the command:

    python manage.py migrate
  5. Now open project\settings.py and modify the line:
    WSGI_APPLICATION = 'djhello.wsgi.application'

    To

    WSGI_APPLICATION = 'project.wsgi.application'

    Similarly, change the line:

    ROOT_URLCONF = 'djhello.urls'

    To

    ROOT_URLCONF = 'project.urls'

     

  6. Since, Django 1.8, the method of locating templates has been changed. Update your project/settings.py file to this:
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

    Also, if the following lines are found, remove them:

    TEMPLATE_DIRS = (
        os.path.join(BASE_DIR,  'templates'),
    )
  7. Now from the menu, click Run -> Run djhello
  8. Django development server will start and the URL for the project should be:
    http://127.0.0.1:8000/

    Open the browser and execute the above URL. You should see the default welcome page.

  9. Now we will create our first page. For this, first we will create views folder under app. Here we will create our classes and methods that corresponds to URLs. We can safely remove the views.py file for now. We will create all our views under app/views folder.
  10. Next, we will create a file home.py under the views folder. Add following code to this file:
    from django.shortcuts import render
    
    def index(request):
        return render(request, 'home.html')

    The index method here will represent the home page of our site.
    home.html is the template (html) that will be displayed on the web browser.

  11. Next create home.html in the templates folder and add whatever html you want.
  12. Now, open project/urls.py and add following line in the import section:
    from app.views import home
  13. Now, update urlpatterns and make it look like this:
    urlpatterns = [
        url(r'^', home.index),
    ]

    The url( ) function is used to map actual URLs to actions in our controllers.
    The first parameter generally starts with r with means use raw string and no string replacement is required. This is optional and we can avoid it but it is recommended.
    The second parameter specifies the function in a particular view file.

  14. Now if you access the URL:
    http://127.0.0.1:8000/

    You will see your own html output this time.