How To Debug Django Project With Eclipse And PyDev

When you develop 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 Django project easily and fix error 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 Django in wizards input box to select PyDev Django Project as below and click Next button.create pydev django project dialog
  2. Input Django project name in the next dialog and select project contents directory then click Next button.
    input pydev django project name
  3. Select sqlite as the project default database. You can also select other database like mysql, oracle etc. Then click Finish button to complete the project creation.
    select sqlite as the pydev django project database

2. Create Django Application.

2.1 Create Django Application.

Now there is only the Django project directory in the eclipse PyDev. You should create a Django application in it.

  1. Open a terminal and go to above Django web project directory.
    $ cd /Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonDjangoProject
  2. Run below command in terminal to create the Django application hello_world.
    $ python manage.py startapp hello_world
  3. Now refresh the Django project folder in eclipse, you can see hello_world folder has been created in it. Please note the hello_world / pages folder and hello_world / urls.py file is created by hand in later chapter.
    django web application directory files list

2.2 Edit DjangoProjectExample / settings.py File.

This file is Django web project configuration file, in this example you should change two sections in this file.

  1. 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',
    ]
  2. Set Django application html template page directory. Django application use html file as template page to render content to client, you should set the template html files saved directory in TEMPLATES section. The DIRS value is string list, you can set multiple directory as html template directory.
    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.

This file is used to map client request url path to different Django application’s urls.py. And the mappings between Django application request url and process view function is 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.

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.

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 hello_word folder.
  2. Create index.html file in pages directory. The html file content is very simple, it display the message sent back from 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. If you set break point in your python source file, the execution will stop at the break point, then you can watch and inspect related variable values.
    right click django project click debug as pydev django project
  2. Below is the web page when browse url http://localhost:8000/hello_world/hello in web browser.
    django hello world example page
(Visited 32 times, 1 visits today)
READ :   How To Install Python Django In Virtual Environment

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.