Hello World Django PyCharm Example

PyCharm is a very popular Python programming IDE. It provides a lot of tools for you to make Python development easy. This article will tell you how to use it to develop Django web application step by step.

The PyCharm IDE is the professional version. If you do not want to buy the professional version, you can read the article How To Make A Website With Python And Django to learn how to create Django project with Eclipse and PyDev ( which is totally free and open-source).

1. Create Django Project In PyCharm.

  1. Open PyCharm, click File —> New Project menu item in the top toolbar.
  2. Then select Django menu item in left navigation menu list, and select the Django project target directory in right panel, and then click Create button.
    create-new-django-project-in-pycharm-
  3. Now you will find the Django project has been created successfully in the project explorer panel. Below Django project files list contains a Django application my_hello_world which will be created later. Now you should not see it because it has not been created.
    pycharm-django-project-files-list

2. Create Django Application my_hello_world.

As you know, one Django project can contain multiple Django applications, now we will run $ python manage.py startapp my_hello_world command in PyCharm to create the my_hello_world application.

  1. Click Tools —> Run manage.py Tasks… menu item to open the python command window at bottom of PyCharm.
    pycharm-tool-run-manage.py-task-menu-item
  2. Then input Django command startapp my_hello_world in the bottom console window and click enter key, you will find the my_hello_world application has been created in the project files list.
    pycharm-tool-run-manage.py-startapp-command-to-create-a-django-application

3. Coding The Django Hello World Example.

  1. Edit DjangoHelloWorld / DjangoHelloWorld / settings.py file and add my_hello_world app at the end of INSTALLED_APPS section.
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'my_hello_world',
    ]
  2. Edit DjangoHelloWorld / DjangoHelloWorld / urls.py file and add a new URL pattern.
    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
    
        # when client request http://127.0.0.1:8000/index/, the project will find the mapped process method in my_hello_world app's urls.py file.
        path('index/', include('my_hello_world.urls'))
    ]
  3. Create urls.py file in DjangoHelloWorld / my_hello_world  folder and add below Python code in it.
    from django.urls import path
    # import views from local directory.
    from . import views
    
    urlpatterns = [
    
        # When user request home page http://localhost:8000/my_hello_world, it will invoke the home function defined in views.py.
        path('', views.index_page, name='index'),
    
    ]
  4. Add index_page view function in DjangoHelloWorld / my_hello_world / views.py file. This view function will return an Html template file index.html back to the client browser
    from django.shortcuts import render
    
    # Create your views here.
    # This function will return and render the home page when url is http://localhost:8000/to_do/.
    
    def index_page(request):
        # Get the index template file absolute path.
        # index_file_path = PROJECT_PATH + '/pages/home.html'
    
        # Return the index file to client.
        return render(request, 'index.html')
  5. Create index.html in DjangoHelloWorld / templates folder and add the below code in it.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Django Hello World Example</title>
    </head>
    <body>
    <h2>
    
        Hello Django! :)
    
    </h2>
    </body>
    </html>

4. Run / Debug Django Hello World Example.

Now all the source code has been created. You need to follow below steps to run or debug it.

  1. Click Run —> Run ( or Run —> Debug )menu item in PyCharm top tool bar to run it.
  2. Select DjangoHelloWorld menu item in the popup menu list to run the Django project.
    select-pycharm-run-target-project
  3. When the application startup, input url http://127.0.0.1:8000/index/ in the web browser, then you will get below web page.
    pycharm-django-hello-world-example-web-page

5. Create Django Project In PyCharm Community Edition.

If you are familiar with PyCharm community edition, and you want to create Django project in it, you can follow below steps.

  1. Install Django in a virtual environment, you can read article How To Install Python Django In Virtual Environment.
  2. Create Django project in command line, you can read article How To Make A Website With Python And Django.
  3. Create Django application in command line, you can read article How To Create / Remove Django Project Application.
  4. Import the Django project into PyCharm community edition, you can read article How To Import Existing Django Project And Enable Django Support In PyCharm.
  5. You just use pycharm to edit source code, and run / debug the Django project application in command line.
0 0 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Sheikh Russel
Sheikh Russel

it was pretty much helpful, thanks a lot dev2qa.com

1
0
Would love your thoughts, please comment.x
()
x