VOOZH about

URL: https://www.geeksforgeeks.org/python/create-word-counter-app-using-django/

⇱ Create Word Counter App using Django - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Create Word Counter App using Django

Last Updated : 8 Apr, 2026

Our task is to build a simple Django application that counts the number of words in a given text. This is a great beginner project if you have some basic knowledge of Django.

Refer to the below article to know about basics of Django.

Step 1: Create the Word Counting Logic in Views

First, we create a function called counter in views.py which will handle the logic for counting words:

Explanation:

  • When the user submits text via POST, we check if the input is not empty.
  • We then split the text by spaces and count the resulting words.
  • If successful, we pass the word count and original text back to the template.
  • On GET requests or empty input, we simply render the form without results.

Step 2: Configure URL Patterns

In your app's urls.py, map the URL /counter to your counter view:

Then include your app URLs in the main project urls.py:

Step 3: Create the HTML Template

Create a file called counter.html inside your app’s templates folder and add the following code:

Notes:

  • The {% csrf_token %} tag is essential for POST requests to pass Django’s CSRF protection.
  • We conditionally display the word count only when the counting process has succeeded (i is true).
  • {{ word|pluralize }} adds an “s” when the word count is more than one for proper grammar.

Step 4: Run Your Application

Make sure your Django server is running:

python manage.py runserver

Output:

👁 TemplateDoesNotExist at /counter
Comment