How To Debug Django Project With Eclipse And PyDev

When you develop a python Django web project, you may find it is not easy to debug the source code even edit the source files. But with Eclipse PyDev, you can create and debug the Django project easily and fix errors quickly. This article will tell you how to use Eclipse PyDev to create and debug Django project files.

1. Create Django Project With PyDev Django Wizard.

  1. Open eclipse, click File —> New —> Others menu item. Then input the keyword Django in the Wizard’s input box to select PyDev —> PyDev Django Project and click the Next button to go to the create new PyDev Django Project wizard dialog.
  2. Input Django Project name in the create new PyDev Django Project dialog and select Project contents directory then click Next button.
  3. In the Django Settings —> Basic Django Settings wizard dialog Database settings —> Database Engine section, select sqlite3 as the project default database. You can also select another database like MySQL, Oracle, etc. Then click the Finish button to complete the project creation.

2. Create Django Application.

2.1 Create Django Application.

  1. Now there is only the Django project directory in the eclipse PyDev. You should create a Django application in it.
  2. Open a terminal and go to the above Django web project directory.
    $ cd /Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonDjangoProject
  3. Run below command in terminal to create the Django application hello_world.
    $ python manage.py startapp hello_world
  4. Now refresh the Django project folder in eclipse, you can see the hello_world folder has been created in it. Please note the hello_world/pages folder and hello_world/urls.py file are created by hand in the later chapter.
    C:\WORKSPACE\WORK\DEV2QA.COM-EXAMPLE-CODE\PYTHONDJANGOPROJECT
    │   .project
    │   .pydevproject
    │   db.sqlite3
    │   manage.py
    │
    ├───DjangoProjectExample
    │       settings.py
    │       urls.py
    │       wsgi.py
    │       __init__.py
    │
    └───hello_world
        │   admin.py
        │   apps.py
        │   models.py
        │   tests.py
        │   urls.py
        │   views.py
        │   __init__.py
        │
        ├───migrations
        │       __init__.py
        │
        └───pages
                index.html

2.2 Edit DjangoProjectExample/settings.py File.

  1. This file is a Django web project configuration file, in this example, you should change two sections in this file.
  2. Add hello_world application in the INSTALLED_APPS section as below.
    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        
        # Add hello_world application.
        'hello_world',
    ]
  3. Set Django application Html template page directory. Django application uses Html files as the template page to render content to the client, you should set the template Html files saved directory in the TEMPLATES section. The DIRS value is a string list, you can set multiple directories as Html template directories.
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            # If DIRS value is empty or '', it may throw TemplateDoesNotExist error.
            'DIRS': ['/Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonDjangoProject/'],
            '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',
                ],
            },
        },
    ]

2.3 Edit DjangoProjectExample/urls.py File.

  1. This file is used to map client request url paths to different Django application’s urls.py.
  2. And the mappings between Django application request url and process view function are defined in the application’s urls.py file.
    from django.contrib import admin
    from django.urls import path,include
    from django.conf.urls import url
    import hello_world
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        # When the request url is http://localhost:8000/hello_world/ which application context path is hello_world, 
        # then Django server will use request url path and process view function mappings defined in hello_world/urls.py.    
        url('^hello_world/', include(('hello_world.urls','hello_world'), namespace='hello_world')
    ]

2.4 Add hello_world/urls.py File.

  1. hello_world/urls.py.
    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import url
    from . import views
    import hello_world
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        # When request url is http://localhost:8000/hello_world/hello, it will invoke hello_world function defined in hello_world/views.py.
        # The first argument is the path relative to django application context path.
        path('hello', views.hello_world, name='helo_world'),
    ]

2.5 Edit hello_world/views.py File.

  1. This file contains python function which process client request by request url path. The request url path to function mapping is defined in hello_world/urls.py.
    from django.shortcuts import render
    # This function is invoked to return html template page to client when the request url is http://localhost:8000/hello_world/hello
    def hello_world(request):
        # The html file path relative to TEMPLATE DIRS directory defined in DjangoProjectExample / settings.py file..
        hello_world_file_path = 'hello_world/pages/index.html'
        # The context object will send back to client, it is a dictionary object contains a Message.
        context = {'Message' : 'Welcome to Django world.'}
        return render(request, hello_world_file_path, context)

2.6 Create Html Template Files.

  1. Create directory pages in the hello_world folder.
  2. Create an index.html file in the pages directory. The Html file content is very simple, it displays the message sent back from the server.
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Python Django Hello World</title>
    </head>
    <body>
    {{Message}}
    </body>
    </html>

3. Run / Debug Django Application.

  1. Right-click the Django project, then click Run As / Debug As —> PyDev:Django menu item in the popup menu list. Then you can see the Django server start information in the console.
  2. If you set a breakpoint in your python source file, the execution will stop at the breakpoint, then you can watch and inspect related variable values.
  3. Below is the web page content when browsing url http://localhost:8000/hello_world/hello in a web browser.
    Welcome to Django world.

1 thought on “How To Debug Django Project With Eclipse And PyDev”

  1. It’s ok, but how you debug this, if you clone this project from git?

    Can you explain the steps to debug cloned project?

    Thx

Leave a Comment

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.