Skip to content

abdu1918sr/Advanced_programming_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Polling Project - Real-Time Poll Application

A modern, real-time polling application built with Django and WebSockets. Users can create polls, vote on them, and see live results update across all connected clients.

🎯 Features

  • Real-Time Updates: Live poll results powered by Django Channels and WebSockets
  • User Authentication: Sign up and login with django-allauth (supports Google OAuth)
  • Create & Vote: Create custom polls and vote on existing ones
  • Live Results Dashboard: Beautiful, interactive results page with live vote counts and charts
  • Responsive Design: Modern UI with Bootstrap styling
  • Vote Prevention: Users can only vote once per poll with session tracking

πŸ› οΈ Tech Stack

  • Backend: Django 5.2.7
  • Real-Time: Django Channels 4.0.0, Daphne 4.1.2
  • Frontend: HTML5, JavaScript, Bootstrap 5, Chart.js
  • Authentication: django-allauth with Google OAuth
  • WebSocket Server: Daphne ASGI server
  • Channel Layer: In-memory (development) or Redis (production)
  • Database: SQLite3

πŸ“‹ Requirements

  • Python 3.8+
  • Redis (optional, for production scaling)

πŸš€ Installation

  1. Clone the repository

    git clone https://github.com/abdu1918sr/Advanced_programming_project.git
    cd polling_project
  2. Create a virtual environment

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Apply migrations

    python manage.py migrate
  5. Create a superuser (optional)

    python manage.py createsuperuser
  6. Load demo data (optional)

    python manage.py seed_demo

πŸƒ Running the Application

Development Server (with WebSocket support)

Run Daphne ASGI server on Windows:

daphne -b 0.0.0.0 -p 8000 polling_project.asgi:application

Or on Linux/Mac:

daphne -b 127.0.0.1 -p 8000 polling_project.asgi:application

Then open your browser to: http://localhost:8000

Using Django's Built-in Server (HTTP only - WebSockets won't work)

python manage.py runserver

⚠️ Note: WebSocket functionality requires Daphne. Django's development server only supports HTTP.

πŸ“ Project Structure

polling_project/
β”œβ”€β”€ manage.py                 # Django management script
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ db.sqlite3               # SQLite database
β”œβ”€β”€ README.md                # This file
β”œβ”€β”€ polling_project/         # Main project settings
β”‚   β”œβ”€β”€ settings.py          # Django configuration
β”‚   β”œβ”€β”€ asgi.py              # ASGI configuration for WebSockets
β”‚   β”œβ”€β”€ wsgi.py              # WSGI configuration
β”‚   └── urls.py              # URL routing
└── polls/                   # Main app
    β”œβ”€β”€ models.py            # Question and Choice models
    β”œβ”€β”€ views.py             # View functions
    β”œβ”€β”€ consumers.py         # WebSocket consumer
    β”œβ”€β”€ routing.py           # WebSocket URL routing
    β”œβ”€β”€ forms.py             # Django forms
    β”œβ”€β”€ urls.py              # App URL patterns
    β”œβ”€β”€ admin.py             # Django admin config
    β”œβ”€β”€ templates/
    β”‚   └── polls/
    β”‚       β”œβ”€β”€ base.html    # Base template
    β”‚       β”œβ”€β”€ index.html   # Poll list
    β”‚       β”œβ”€β”€ detail.html  # Vote page
    β”‚       β”œβ”€β”€ results.html # Live results with WebSocket
    β”‚       β”œβ”€β”€ create.html  # Create poll
    β”‚       β”œβ”€β”€ login.html   # Login page
    β”‚       β”œβ”€β”€ about.html   # About page
    β”‚       └── contact.html # Contact page
    └── management/
        └── commands/
            └── seed_demo.py # Demo data seeding

πŸ”§ Configuration

Google OAuth Setup

  1. Go to Google Cloud Console
  2. Create a new project
  3. Enable Google+ API
  4. Create OAuth 2.0 credentials (Web application)
  5. Set authorized redirect URIs to your domain
  6. Add to environment variables:
    export GOOGLE_CLIENT_ID="your-client-id"
    export GOOGLE_CLIENT_SECRET="your-client-secret"

Redis Configuration (Production)

For scalable WebSocket support across multiple servers:

pip install channels-redis

Set environment variable:

export REDIS_URL="redis://localhost:6379"

πŸ’‘ Key Features Explained

Real-Time Polls

  • Uses Django Channels to manage WebSocket connections
  • When a vote is cast, all connected clients receive live updates
  • Vote counts and progress bars update instantly

Vote Prevention

  • Session-based tracking prevents users from voting twice
  • Displays "You already voted" message
  • Users can still view results after voting

Live Results Chart

  • Interactive Chart.js bar chart
  • Updates in real-time as votes come in
  • Color-coded choices with progress bars

🌐 URLs

  • / - Home (poll list)
  • /polls/<id>/ - Vote on a poll
  • /polls/<id>/results/ - View live results
  • /polls/create/ - Create new poll
  • /accounts/login/ - Login page
  • /accounts/logout/ - Logout
  • /admin/ - Django admin panel
  • /ws/polls/<id>/ - WebSocket endpoint (real-time updates)

πŸ“Š API/WebSocket

WebSocket Connection

const socket = new WebSocket(`ws://${host}/ws/polls/${question_id}/`);

Incoming Messages

{
  "type": "init",
  "choices": [
    {"id": 1, "choice_text": "Option 1", "votes": 5},
    {"id": 2, "choice_text": "Option 2", "votes": 3}
  ]
}

πŸ› Troubleshooting

WebSocket Not Working

  • Ensure Daphne is running, not Django's development server
  • Check browser console for connection errors (F12 β†’ Console)
  • Verify WebSocket URL format in browser DevTools (Network tab)

Database Errors

  • Run migrations: python manage.py migrate
  • Reset database: python manage.py migrate --reset

πŸ‘€ Author

Abdu1918sr

🀝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.


Last Updated: January 2026

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors