Hello World Django PyCharm Example

PyCharm is a very popular Python programming IDE. It provide 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 want to know how to create Django project with eclipse and PyDev, you can refer How To Make A Website With Python And Django.

1. Create Django Project In PyCharm.

  1. Open PyCharm, click File —> New Project menu item in top tool bar.
  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, 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 a 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 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
(Visited 389 times, 8 visits today)
READ :   How To Pass Parameters To View Via Url In Django

Leave a Reply

Your email address will not be published. Required fields are marked *

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