Create Word Counter App using Django
0 428
Introduction to Building a Word Counter App Using Django
A Word Counter App is a great starter project in Django that demonstrates handling form input, processing data on the server, and rendering results in a template. This app takes user text input and returns a breakdown of word frequency, helping you understand request flow and template rendering in Django.
Project Overview
The app prompts users to enter a block of text in a form. Upon submission, the server processes it—splitting the text into words, counting their frequency, and displaying the results in an organized table or list within the webpage.
Step 1: Scaffold Your Django Project
Start by creating a new Django project and app:
django-admin startproject wordcount_project
cd wordcount_project
python manage.py startapp counter
Then, add 'counter' to the INSTALLED_APPS in your project's settings.py.
Step 2: Create the Input Form
Define a form in counter/forms.py using Django’s forms framework:
from django import forms
class TextForm(forms.Form):
content = forms.CharField(
widget=forms.Textarea(attrs={'rows': 5, 'placeholder': 'Paste your text here'})
)
Step 3: Add View Logic to Handle Count
In counter/views.py, add a view to process the submitted text:
from django.shortcuts import render
from .forms import TextForm
from collections import Counter
def word_count_view(request):
form = TextForm(request.POST or None)
word_counts = None
if form.is_valid():
text = form.cleaned_data['content']
words = text.split()
word_counts = Counter(words)
return render(request, 'counter/word_count.html', {
'form': form,
'word_counts': word_counts
})
Step 4: Create the Template
Create counter/templates/counter/word_count.html to render the form and results:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Count Words</button>
</form>
{% if word_counts %}
<h3>Word Frequency</h3>
<table>
<tr><th>Word</th><th>Count</th></tr>
{% for word, count in word_counts.items %}
<tr><td>{{ word }}</td><td>{{ count }}</td></tr>
{% endfor %}
</table>
{% endif %}
Step 5: Define the URL Pattern
Add the view path in counter/urls.py:
from django.urls import path
from .views import word_count_view
urlpatterns = [
path('', word_count_view, name='word-count'),
]
Include it in the main project's urls.py:
from django.urls import include, path
urlpatterns = [
path('', include('counter.urls')),
]
Step 6: Try It Out
Run your Django development server:
python manage.py runserver
Visit http://127.0.0.1:8000/ in your browser, paste some text, and click "Count Words". The app will display a frequency table of all words entered.
Enhancements You Can Explore
- Add sorting options to display frequent words first.
- Ignore case sensitivity or remove punctuation.
- Show graphs using chart libraries like Chart.js.
- Store submissions in the database for history or analytics.
Conclusion
Creating a Word Counter App using Django is a simple yet effective way to learn Django's core features: forms, views, templates, and templates rendering logic. It shows how to manage user input, server-side processing, and result presentation in a clean, interactive application.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:


Comments
Waiting for your comments