Open In App
Related Articles

Create Word Counter app using Django

Improve Article
Save Article
Like Article

In this article, we are going to make a simple tool that counts a number of words in text using Django. Before diving into this topic you need to have some basic knowledge of Django.

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

Now moving forward to make our wordcounter, first, we need to create a function where we can write the logic for counting words.


from django.shortcuts import render
# defining function for wordcounter
def counter(request):
    # checking if method is POST or not
    if request.method == 'POST':
        # taking text input
        text = request.POST['texttocount']
        # checking weather text is empty
        # or not
        if text != '':
            # splitting the text and taking length
            # of that
            word = len(text.split())
            # defining boolean so that we can keep
            # track of process later
            i = True
            # returning HTML page with data, if calculated
            # successfully
            return render(request, 'counter.html',
                          {'word': word, 'text': text, 'i': i, 'on': 'active'})
            # returning HTML page without data, if any
            # error occurs
            return render(request, 'counter.html', {'on': 'active'})
        # returning HTML page if request.method is not POST
        return render(request, 'counter.html', {'on': 'active'})


First, we created a counter function that will receive text input from the frontend. After that, it will check if the text is empty or not. Then it will split the words from the text and measure the length of them. Also, store that length into a variable named “word”. We have also created one Boolean which will be used to keep track of the process later. If this process gets completed successfully then it will return an HTML page with data stored in word & text. And if the process can’t be completed successfully then it will return the same HTML page without any data.

After creating the function, we need to define a URL for that. We will be using http://localhost:8000/counter here. For that open your folder and write the following code.


from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('counter',views.counter, name='counter'),

  After this, we need to create an HTML page for that. Here you can use your Designing skills and prepare your own design and name it counter.html.  Over here we will show the logic which you can frontend. 

First of all, we will check that Boolean is true or false to know the process completed successfully or not. If that is true then we can move forward. Later on, we will use the word variable to print output with the help of Jinja format. Then we will create form to take data from user which will be sent inside our counter function. Since we are using the POST method to handle requests, we have to give  {% csrf_token %} to that.



{% if i %}
<div class="mb-2" style="border-style: groove;border-radius: 10px;">
  <div class="card-header">
    <p class="m-0" style="font-size: 20px;">{{word}} Word's in that text</p>
{% endif %}
<div class="col-12 pb-3" style="background-color:  #c2d6d6; border: 2px; border-style: groove; border-radius: 10px;">
  <form action="counter" method="POST"> {% csrf_token %}
    <div class="form-group">
      <label for="text1">Paste you're text</label>
      <textarea class="form-control" placeholder="Paste you're text here to count word and character" 
                name="texttocount" id="text1" rows="8" style="background-color: #f0f5f5;">{{text}}</textarea>
    <!-- ads section start-->
    <div class="text-center">
    <!-- ads section end -->
    <div class="form-group text-center">
      <button type="submit" class="btn btn-primary w-25" >Run Counter</button>


TemplateDoesNotExist at /counter

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 25 May, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials