diff --git a/.gitignore b/.gitignore index 68bc17f..4513501 100644 --- a/.gitignore +++ b/.gitignore @@ -58,7 +58,7 @@ cover/ # Django stuff: *.log local_settings.py -db.sqlite3 +# db.sqlite3 db.sqlite3-journal # Flask stuff: diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..4249e6f Binary files /dev/null and b/db.sqlite3 differ diff --git a/mysite/settings.py b/mysite/settings.py index ed45396..7a27f5c 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ """ from pathlib import Path +from decouple import config # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -20,7 +21,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-ij$9jv9o30j(51=l)ho^l+x&q9)n77i1vt%j()%9=(ohh(b*!^' +SECRET_KEY = config('SECRET_KEY', default='fake-secret-key') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -31,6 +32,7 @@ ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ + "polls.apps.PollsConfig", 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -53,15 +55,15 @@ ROOT_URLCONF = 'mysite.urls' TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [BASE_DIR / "templates"], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, @@ -115,7 +117,8 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.2/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = '/static/' +STATICFILES_DIRS = [BASE_DIR] # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/mysite/urls.py b/mysite/urls.py index 79d5460..b5d5113 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -1,22 +1,10 @@ -""" -URL configuration for mysite project. - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/4.2/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" from django.contrib import admin -from django.urls import path +from django.urls import include, path + +from polls.views import HomeView urlpatterns = [ + path('', HomeView.as_view(), name='home'), + path("polls/", include("polls.urls")), path('admin/', admin.site.urls), ] diff --git a/polls/__init__.py b/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/admin.py b/polls/admin.py new file mode 100644 index 0000000..c2ce0aa --- /dev/null +++ b/polls/admin.py @@ -0,0 +1,23 @@ +from django.contrib import admin + +from .models import Choice, Question + + +class ChoiceInline(admin.TabularInline): + model = Choice + extra = 3 + + +class QuestionAdmin(admin.ModelAdmin): + fieldsets = [ + (None, {"fields": ["question_text"]}), + ("Date information", {"fields": ["pub_date"], "classes": ["collapse"]}), + ] + list_display = ["question_text", "pub_date", "was_published_recently"] + inlines = [ChoiceInline] + list_filter = ["pub_date"] + search_fields = ["question_text"] + + +admin.site.register(Question, QuestionAdmin) + \ No newline at end of file diff --git a/polls/apps.py b/polls/apps.py new file mode 100644 index 0000000..1dffd05 --- /dev/null +++ b/polls/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig + + +class PollsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'polls' + \ No newline at end of file diff --git a/polls/migrations/0001_initial.py b/polls/migrations/0001_initial.py new file mode 100644 index 0000000..1993212 --- /dev/null +++ b/polls/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.4 on 2023-08-28 11:51 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('question_text', models.CharField(max_length=200)), + ('pub_date', models.DateTimeField(verbose_name='date published')), + ], + ), + migrations.CreateModel( + name='Choice', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('choice_text', models.CharField(max_length=200)), + ('votes', models.IntegerField(default=0)), + ('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')), + ], + ), + ] diff --git a/polls/migrations/__init__.py b/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/models.py b/polls/models.py new file mode 100644 index 0000000..7252b32 --- /dev/null +++ b/polls/models.py @@ -0,0 +1,76 @@ +""" +This module defines the models for the polls app. + +It includes the Question and Choice models, which represent poll questions +and the choices associated with them. These models are used to store and +get poll data in the database. + +Attributes: + None +""" + +import datetime + +from django.db import models +from django.utils import timezone +from django.contrib import admin + + +class Question(models.Model): + """ + Represents a poll question. + + Attributes: + question_text (str): The text of the poll question. + pub_date (datetime): The date and time when the question was published. + """ + + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField("date published") + + def was_published_recently(self): + """ + Checks if the question was published recently or not. + + Returns: + bool: True if the question was published within the last day, else False. + """ + now = timezone.now() + return now - datetime.timedelta(days=1) <= self.pub_date <= now + + @admin.display( + boolean=True, + ordering="pub_date", + description="Published recently?", + ) + def was_published_recently(self): + now = timezone.now() + return now - datetime.timedelta(days=1) <= self.pub_date <= now + + def __str__(self): + """ + Returns a string representation of the question. + """ + return self.question_text + + +class Choice(models.Model): + """ + Represents a choice for a poll question. + + Attributes: + question (Question): The poll question to which the choice belongs. + choice_text (str): The text of the choice. + votes (int): The number of votes the choice has received. + """ + + question = models.ForeignKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + + def __str__(self): + """ + Returns a string representation of the choice. + """ + return self.choice_text + \ No newline at end of file diff --git a/polls/static/polls/base.css b/polls/static/polls/base.css new file mode 100644 index 0000000..afd26f9 --- /dev/null +++ b/polls/static/polls/base.css @@ -0,0 +1,191 @@ +/*! NAVBAR */ + +header { + background-color: #1C1C1C; + color: #fff; + padding: 20px; + text-align: center; + display: flex; + justify-content: center; + align-items: center; + box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); + border-radius: 10px; +} + +.nav-container { + display: flex; + justify-content: center; + align-items: center; +} + +.nav-left h1 a { + text-decoration: none; + color: #fff; + font-size: 24px; +} + + +.nav-right ul { + list-style: none; + padding: 0; + margin: 0; + display: flex; + align-items: center; +} + +.nav-right li { + margin: 0 10px; +} + +.nav-right a { + text-decoration: none; + color: #fff; + font-weight: bold; + padding: 10px 20px; + border: 2px solid #fff; + border-radius: 5px; + transition: background-color 0.3s ease, color 0.3s ease; +} + +.nav-right a:hover { + background-color: #fff; + color: #007bff; +} + +/*! HOME AND POLL CARD */ + +.hero-section { + background-size: cover; + background-position: center; + text-align: center; + color: #1c1c1c; +} + +.hero-content { + max-width: 800px; + margin: 0 auto; +} + +h1 { + font-size: 36px; + margin-bottom: 20px; +} + +.polls-section { + background-size: cover; + background-position: center; + text-align: center; + color: #1c1c1c; +} + +.poll-cards { + display: flex; + flex-wrap: wrap; + gap: 20px; + justify-content: center; + margin-top: 30px; +} + +.poll-card { + background-color: #fff; + border: 1px solid #e0e0e0; + padding: 20px; + border-radius: 5px; + box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.1); + transition: transform 0.3s ease; + cursor: pointer; +} + +.poll-card:hover { + transform: translateY(-5px); +} + +/*! DETAILED */ + +.poll-details { + padding: 30px; + background-color: #fff; + border-radius: 10px; + box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.1); +} + +.poll-form { + text-align: center; +} + +.poll-question { + font-size: 24px; + margin-bottom: 20px; +} + +.error-message { + color: red; + margin-bottom: 10px; +} + +.choice { + display: flex; + align-items: center; + margin: 10px 0; +} + +.choice input[type="radio"] { + margin-right: 10px; +} + +.choice-text { + font-size: 18px; +} + +.vote-button { + background-color: #007bff; + color: #fff; + padding: 10px 20px; + border: none; + border-radius: 5px; + cursor: pointer; + transition: background-color 0.3s ease, color 0.3s ease; +} + +.vote-button:hover { + background-color: #0056b3; +} + +/*! RESULT */ + +.poll-results { + text-align: center; + padding: 30px; + background-color: #fff; + border-radius: 10px; + box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.1); +} + +.poll-question { + font-size: 24px; + margin-bottom: 20px; +} + +.choice-list { + list-style: none; + padding: 0; + margin: 20px 0; + text-align: left; +} + +.choice-item { + font-size: 18px; + margin: 10px 0; +} + +.vote-again { + display: inline-block; + margin-top: 20px; + text-decoration: none; + color: #007bff; + transition: color 0.3s ease; +} + +.vote-again:hover { + color: #0056b3; +} \ No newline at end of file diff --git a/polls/static/polls/images/background.jpg b/polls/static/polls/images/background.jpg new file mode 100644 index 0000000..c47c004 Binary files /dev/null and b/polls/static/polls/images/background.jpg differ diff --git a/polls/static/polls/style.css b/polls/static/polls/style.css new file mode 100644 index 0000000..e69de29 diff --git a/polls/templates/admin/base_site.html b/polls/templates/admin/base_site.html new file mode 100644 index 0000000..7997e66 --- /dev/null +++ b/polls/templates/admin/base_site.html @@ -0,0 +1,12 @@ +{% extends "admin/base.html" %} + +{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} + +{% block branding %} +
+{% if user.is_anonymous %} + {% include "admin/color_theme_toggle.html" %} +{% endif %} +{% endblock %} + +{% block nav-global %}{% endblock %} \ No newline at end of file diff --git a/polls/templates/polls/base.html b/polls/templates/polls/base.html new file mode 100644 index 0000000..ae54f87 --- /dev/null +++ b/polls/templates/polls/base.html @@ -0,0 +1,25 @@ +{% load static %} + + +Explore and participate in our weird poll questions.
+Total number of polls: {{ total_polls }}
+No polls are available.
+ {% endif %} +Published on: {{ question.pub_date|date:"F j, Y" }}
+No polls are available.
+ {% endif %} +