Django-Poll-Application

The Django Poll Application

This repository hosts a poll application that was built with Django, designed to showcase fundamental Django concepts like models, views, templates, and the admin interface.

Table of Contents

User Interface

Homepage

User Interface 1

Poll Details

User Interface 2

Poll Results

User Interface 3

Admin Interface

User Interface 4

Features

Technologies Used

Installation

  1. Clone the Repository: Clone the repository using the Code button in the repository’s main GitHub page.

  2. Create a Virtual Environment:
    python -m venv .venv
    
  3. Activate the Virtual Environment:
    source .venv/bin/activate  # Linux/macOS
    .\venv\Scripts\activate    # Windows
    
  4. Run Migrations:
    python manage.py makemigrations
    python manage.py migrate
    
  5. Create Admin User:
    python manage.py createsuperuser
    
  6. Start the Server:
    python manage.py runserver
    

Important: Remember to change the Django production secret key and set DEBUG to True in PollApplication/settings.py order to run the server:

SECRET_KEY = 'your_secret_key'

DEBUG = True

Also, as you develop the app, every time you make changes to the data in the back end, be sure that you migrate them to the SQLite database using the following commands:

python manage.py makemigrations
python manage.py migrate

Then start the server again and you’ll be all set!

Usage

  1. Access the Admin Interface: Go to http://127.0.0.1:8000/admin/ and log in with your superuser credentials.
  2. Create Polls:
    • Click “Polls” or “Questions” in the admin interface.
    • Click “Add Question” and fill out the question text and publication date.
    • Click “Save and continue editing” to add answer choices.
    • Mark the correct answer using the checkbox.
  3. View Polls: Visit the homepage (http://127.0.0.1:8000/polls/) to see the list of available polls.
  4. Vote: Click on a poll to view details and select your answer.
  5. View Results: After voting, you’ll be redirected to the results page, where you can see the vote distribution and whether you answered correctly.

Project Structure

Customization

REST API Usage

This app also includes a REST API built with Django REST Framework (DRF). Here are the available endpoints:

Endpoint Methods Description Authentication Required
/api/questions/ GET, POST, PUT, DELETE List/create/update/delete questions. âś…
/api/questions/<id>/ GET, PUT, DELETE Retrieve/update/delete a specific question. âś…
/api/choices/ GET, POST, PUT, DELETE List/create/update/delete choices. âś…
/api/choices/<id>/ GET, PUT, DELETE Retrieve/update/delete a specific choice. âś…

Example Usage (with curl):

# Get all questions
curl http://127.0.0.1:8000/api/questions/

# Get a specific question
curl http://127.0.0.1:8000/api/questions/1/

For example, if you run curl http://127.0.0.1:8000/api/questions/, you will get this output:

[{"id":3,"question_text":"huhu","pub_date":"2024-06-10T07:21:01Z"},{"id":4,"question_text":"hehehehe","pub_date":"2024-06-11T15:01:59Z"},{"id":5,"question_text":"What is the capital of France?","pub_date":"2024-06-11T15:11:04Z"}]

You can also directly visit the API endpoints in your browser:

API Interface

Authentication: To use POST, PUT, or DELETE methods, you’ll need a valid token, typically obtained after user login. Include this token in the Authorization header:

curl -X POST -H "Authorization: Token <your_token>" -H "Content-Type: application/json" -d '{"question_text": "Is this a new question?", "pub_date": "2023-12-12T12:00:00Z"}' http://127.0.0.1:8000/api/questions/

Contributions

Contributions are welcome! Feel free to open issues or submit pull requests. Feel free to also fork and customize this repo to fit your needs.

License

This project is licensed under the MIT License.

Contact

If you have any questions about this project or Django (or even the Django REST Framework) in general, feel free to contact me! I’ll be happy to answer any questions you might have (hopefully I’ll know the answers to them…)


Thank you for visiting today!