diff --git a/polls/migrations/0011_remove_vote_question.py b/polls/migrations/0011_remove_vote_question.py new file mode 100644 index 0000000..ab6c9e9 --- /dev/null +++ b/polls/migrations/0011_remove_vote_question.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.4 on 2023-09-14 12:47 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('polls', '0010_sentimentvote'), + ] + + operations = [ + migrations.RemoveField( + model_name='vote', + name='question', + ), + ] diff --git a/polls/migrations/0012_vote_question.py b/polls/migrations/0012_vote_question.py new file mode 100644 index 0000000..4b4c620 --- /dev/null +++ b/polls/migrations/0012_vote_question.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.4 on 2023-09-14 13:15 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('polls', '0011_remove_vote_question'), + ] + + operations = [ + migrations.AddField( + model_name='vote', + name='question', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='polls.question'), + ), + ] diff --git a/polls/migrations/0013_alter_vote_question.py b/polls/migrations/0013_alter_vote_question.py new file mode 100644 index 0000000..9bb9277 --- /dev/null +++ b/polls/migrations/0013_alter_vote_question.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.4 on 2023-09-14 13:15 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('polls', '0012_vote_question'), + ] + + operations = [ + migrations.AlterField( + model_name='vote', + name='question', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question'), + ), + ] diff --git a/polls/models.py b/polls/models.py index a9c0508..10be79f 100644 --- a/polls/models.py +++ b/polls/models.py @@ -172,8 +172,8 @@ class Question(models.Model): vote.update(vote_types=True) self.save() else: - return 'already_upvoted' - return 'ok' + return False + return True def downvote(self, user): @@ -187,8 +187,8 @@ class Question(models.Model): vote.update(vote_types=False) self.save() else: - return 'already_downvoted' - return 'ok' + return False + return True class Choice(models.Model): diff --git a/polls/templates/polls/base.html b/polls/templates/polls/base.html index fc6124b..fa30709 100644 --- a/polls/templates/polls/base.html +++ b/polls/templates/polls/base.html @@ -6,10 +6,6 @@ - diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index d870900..fb8fdb2 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -3,8 +3,13 @@ - 📝KU POLL - + + {% if user.is_authenticated %} + 📝KU POLL | 👋 Hi! {{ user.username }} + {% else %} + 📝KU POLL + {% endif %} + {% comment %} diff --git a/polls/templates/registration/login.html b/polls/templates/registration/login.html index 99361b9..398cecb 100644 --- a/polls/templates/registration/login.html +++ b/polls/templates/registration/login.html @@ -7,7 +7,7 @@ - + Sign In {% csrf_token %} diff --git a/polls/templates/registration/signup.html b/polls/templates/registration/signup.html index 0bac13d..6f9ba91 100644 --- a/polls/templates/registration/signup.html +++ b/polls/templates/registration/signup.html @@ -7,7 +7,7 @@ - + Sign Up {% csrf_token %} diff --git a/polls/tests.py b/polls/tests.py deleted file mode 100644 index db9a67f..0000000 --- a/polls/tests.py +++ /dev/null @@ -1,224 +0,0 @@ -import datetime - -from django.test import TestCase -from django.utils import timezone -from django.urls import reverse -from django.contrib.auth.models import User - -from .models import Question - - -class QuestionModelTests(TestCase): - def test_was_published_recently_with_future_question(self): - """ - was_published_recently() returns False for questions whose pub_date - is in the future. - """ - time = timezone.now() + datetime.timedelta(days=30) - future_question = Question(pub_date=time) - self.assertIs(future_question.was_published_recently(), False) - - def test_was_published_recently_with_old_question(self): - """ - was_published_recently() returns False for questions whose pub_date - is older than 1 day. - """ - time = timezone.now() - datetime.timedelta(days=1, seconds=1) - old_question = Question(pub_date=time) - self.assertIs(old_question.was_published_recently(), False) - - def test_was_published_recently_with_recent_question(self): - """ - was_published_recently() returns True for questions whose pub_date - is within the last day. - """ - time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) - recent_question = Question(pub_date=time) - self.assertIs(recent_question.was_published_recently(), True) - - def test_is_published_with_future_question(self): - """ - is_published() should return False for questions whos pub_date is in the - future. - """ - future_date = timezone.now() + datetime.timedelta(days=30) - future_question = Question(pub_date=future_date) - self.assertIs(future_question.is_published(), False) - - def test_default_pub_date(self): - """ - Questions with the default pub_date (now) are displayed on the index page. - """ - question = Question.objects.create(question_text="Default pub date question.") - - response = self.client.get(reverse("polls:index")) - self.assertQuerySetEqual( - response.context["latest_question_list"], - [question], - ) - - def test_is_published_with_past_question(self): - """ - is_published() should return True for questions whose pub_date is in the - past. - """ - past_date = timezone.now() - datetime.timedelta(days=1) - past_question = Question(pub_date=past_date) - self.assertIs(past_question.is_published(), True) - - def test_can_vote_with_question_not_ended(self): - """ - can_vote() should return True for questions that are published and have not - ended. - """ - pub_date = timezone.now() - datetime.timedelta(hours=1) - end_date = timezone.now() + datetime.timedelta(hours=1) - question = Question(pub_date=pub_date, end_date=end_date) - self.assertIs(question.can_vote(), True) - - def test_can_vote_with_question_ended(self): - """ - can_vote() should return False for questions that are published but have - ended. - """ - pub_date = timezone.now() - datetime.timedelta(hours=2) - end_date = timezone.now() - datetime.timedelta(hours=1) - question = Question(pub_date=pub_date, end_date=end_date) - self.assertIs(question.can_vote(), False) - - def test_can_vote_with_question_no_end_date(self): - """ - can_vote() should return True for questions that are published and have no - specified end date. - """ - pub_date = timezone.now() - datetime.timedelta(hours=1) - question = Question(pub_date=pub_date, end_date=None) - self.assertIs(question.can_vote(), True) - - def test_can_vote_with_question_ending_in_future(self): - """ - can_vote() should return True for questions that are published and - the current time is within the allowed voting period. - """ - pub_date = timezone.now() - datetime.timedelta(hours=1) - end_date = timezone.now() + datetime.timedelta(hours=2) - question = Question(pub_date=pub_date, end_date=end_date) - self.assertIs(question.can_vote(), True) - - -def create_question(self, question_text, days, pub_date=None): - """ - Create a question with the given `question_text` and published the - given number of `days` offset to now (negative for questions published - in the past, positive for questions that have yet to be published). - """ - time = timezone.now() + timezone.timedelta(days=days) - if pub_date is not None: - time = pub_date - return Question.objects.create(question_text=question_text, pub_date=time) - - -class QuestionIndexViewTests(TestCase): - def test_no_questions(self): - """ - If no questions exist, an appropriate message is displayed. - """ - response = self.client.get(reverse("polls:index")) - self.assertEqual(response.status_code, 200) - self.assertQuerySetEqual(response.context["latest_question_list"], []) - - def test_past_question(self): - """ - Questions with a pub_date in the past are displayed on the - index page. - """ - question = Question.objects.create(question_text="Past question.") - question.pub_date = timezone.now() - timezone.timedelta(days=30) - question.save() - response = self.client.get(reverse("polls:index")) - self.assertQuerySetEqual( - response.context["latest_question_list"], - [question], - ) - - def test_future_question(self): - """ - Questions with a pub_date in the future aren't displayed on - the index page. - """ - future_question = Question.objects.create(question_text="Future question.") - future_question.pub_date = timezone.now() + timezone.timedelta(days=30) - future_question.save() - response = self.client.get(reverse("polls:index")) - self.assertQuerySetEqual(response.context["latest_question_list"], []) - - def test_future_question_and_past_question(self): - """ - Even if both past and future questions exist, only past questions - are displayed. - """ - past_question = Question.objects.create(question_text="Past question.") - past_question.pub_date = timezone.now() - timezone.timedelta(days=30) - past_question.save() - - future_question = Question.objects.create(question_text="Future question.") - future_question.pub_date = timezone.now() + timezone.timedelta(days=30) - future_question.save() - - response = self.client.get(reverse("polls:index")) - self.assertQuerySetEqual( - response.context["latest_question_list"], - [past_question], - ) - - def test_two_past_questions(self): - """ - The questions index page may display multiple questions. - """ - question1 = Question.objects.create(question_text="Past question 1.") - question1.pub_date = timezone.now() - timezone.timedelta(days=30) - question1.save() - - question2 = Question.objects.create(question_text="Past question 2.") - question2.pub_date = timezone.now() - timezone.timedelta(days=5) - question2.save() - - response = self.client.get(reverse("polls:index")) - self.assertQuerySetEqual( - response.context["latest_question_list"], - [question2, question1], - ) - - -class QuestionDetailViewTests(TestCase): - def test_future_question(self): - """ - The detail view of a question with a pub_date in the future - returns a 404 not found. - """ - future_question = Question.objects.create(question_text="Future question.") - future_question.pub_date = timezone.now() + timezone.timedelta(days=5) - future_question.save() - - user = User.objects.create_user(username="testcase", password="123test123") - self.client.login(username="testcase", password="123test123") - - url = reverse("polls:detail", args=(future_question.id,)) - response = self.client.get(url) - self.assertEqual(response.status_code, 404) - - def test_past_question(self): - """ - The detail view of a question with a pub_date in the past - displays the question's text. - """ - past_question = Question.objects.create(question_text="Past Question.") - past_question.pub_date = timezone.now() - timezone.timedelta(days=5) - past_question.save() - - user = User.objects.create_user(username="testcase", password="123test123") - self.client.login(username="testcase", password="123test123") - - url = reverse("polls:detail", args=(past_question.id,)) - response = self.client.get(url) - self.assertContains(response, past_question.question_text) diff --git a/polls/tests/__init__.py b/polls/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/tests/base.py b/polls/tests/base.py new file mode 100644 index 0000000..1e2db85 --- /dev/null +++ b/polls/tests/base.py @@ -0,0 +1,14 @@ +from django.utils import timezone +from django.contrib.auth.models import User + +from ..models import Question, Vote, Choice + + +def create_question(question_text, day=0): + """ + Create a question with the given `question_text` and published the + given number of `days` offset to now (negative for questions published + in the past, positive for questions that have yet to be published). + """ + time = timezone.now() + timezone.timedelta(days=day) + return Question.objects.create(question_text=question_text, pub_date=time) \ No newline at end of file diff --git a/polls/tests/test_detail_views.py b/polls/tests/test_detail_views.py new file mode 100644 index 0000000..db2f3bc --- /dev/null +++ b/polls/tests/test_detail_views.py @@ -0,0 +1,43 @@ +from django.test import TestCase, Client +from django.utils import timezone +from django.urls import reverse +from django.contrib.auth.models import User + +from .base import create_question + + +class QuestionDetailViewTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.user = User.objects.create_user(username="test_user_123", password="aaa123321aaa") + cls.client = Client() + + def test_future_question(self): + """ + The detail view of a question with a pub_date in the future + returns a 404 not found. + """ + future_question = create_question(question_text="Future question.", day=10) + future_question.save() + + self.client.login(username=self.user.username, password="aaa123321aaa") + + url = reverse("polls:detail", args=(future_question.id,)) + respone = self.client.get(url) + self.assertEqual(respone.status_code, 404) + + def test_past_question(self): + """ + The detail view of a question with a pub_date in the past + displays the question's text. + """ + past_question = create_question(question_text="Past Question.", day=-10) + past_question.save() + + self.client.login(username=self.user.username, password='aaa123321aaa') + + url = reverse("polls:detail", args=(past_question.id,)) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + self.assertContains(response, past_question.question_text) diff --git a/polls/tests/test_index_views.py b/polls/tests/test_index_views.py new file mode 100644 index 0000000..4cf0ad2 --- /dev/null +++ b/polls/tests/test_index_views.py @@ -0,0 +1,77 @@ +from django.test import TestCase +from django.utils import timezone +from django.urls import reverse + +from ..models import Question + + +class QuestionIndexViewTests(TestCase): + def test_no_questions(self): + """ + If no questions exist, an appropriate message is displayed. + """ + response = self.client.get(reverse("polls:index")) + self.assertEqual(response.status_code, 200) + self.assertQuerySetEqual(response.context["latest_question_list"], []) + + def test_past_question(self): + """ + Questions with a pub_date in the past are displayed on the + index page. + """ + question = Question.objects.create(question_text="Past question.") + question.pub_date = timezone.now() - timezone.timedelta(days=30) + question.save() + response = self.client.get(reverse("polls:index")) + self.assertQuerySetEqual( + response.context["latest_question_list"], + [question], + ) + + def test_future_question(self): + """ + Questions with a pub_date in the future aren't displayed on + the index page. + """ + future_question = Question.objects.create(question_text="Future question.") + future_question.pub_date = timezone.now() + timezone.timedelta(days=30) + future_question.save() + response = self.client.get(reverse("polls:index")) + self.assertQuerySetEqual(response.context["latest_question_list"], []) + + def test_future_question_and_past_question(self): + """ + Even if both past and future questions exist, only past questions + are displayed. + """ + past_question = Question.objects.create(question_text="Past question.") + past_question.pub_date = timezone.now() - timezone.timedelta(days=30) + past_question.save() + + future_question = Question.objects.create(question_text="Future question.") + future_question.pub_date = timezone.now() + timezone.timedelta(days=30) + future_question.save() + + response = self.client.get(reverse("polls:index")) + self.assertQuerySetEqual( + response.context["latest_question_list"], + [past_question], + ) + + def test_two_past_questions(self): + """ + The questions index page may display multiple questions. + """ + question1 = Question.objects.create(question_text="Past question 1.") + question1.pub_date = timezone.now() - timezone.timedelta(days=30) + question1.save() + + question2 = Question.objects.create(question_text="Past question 2.") + question2.pub_date = timezone.now() - timezone.timedelta(days=5) + question2.save() + + response = self.client.get(reverse("polls:index")) + self.assertQuerySetEqual( + response.context["latest_question_list"], + [question2, question1], + ) diff --git a/polls/tests/test_question_model.py b/polls/tests/test_question_model.py new file mode 100644 index 0000000..6514de0 --- /dev/null +++ b/polls/tests/test_question_model.py @@ -0,0 +1,105 @@ +import datetime + +from django.test import TestCase +from django.utils import timezone +from django.urls import reverse + +from ..models import Question + + +class QuestionModelTests(TestCase): + def test_was_published_recently_with_future_question(self): + """ + was_published_recently() returns False for questions whose pub_date + is in the future. + """ + time = timezone.now() + datetime.timedelta(days=30) + future_question = Question(pub_date=time) + self.assertIs(future_question.was_published_recently(), False) + + def test_was_published_recently_with_old_question(self): + """ + was_published_recently() returns False for questions whose pub_date + is older than 1 day. + """ + time = timezone.now() - datetime.timedelta(days=1, seconds=1) + old_question = Question(pub_date=time) + self.assertIs(old_question.was_published_recently(), False) + + def test_was_published_recently_with_recent_question(self): + """ + was_published_recently() returns True for questions whose pub_date + is within the last day. + """ + time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) + recent_question = Question(pub_date=time) + self.assertIs(recent_question.was_published_recently(), True) + + def test_is_published_with_future_question(self): + """ + is_published() should return False for questions whos pub_date is in the + future. + """ + future_date = timezone.now() + datetime.timedelta(days=30) + future_question = Question(pub_date=future_date) + self.assertIs(future_question.is_published(), False) + + def test_default_pub_date(self): + """ + Questions with the default pub_date (now) are displayed on the index page. + """ + question = Question.objects.create(question_text="Default pub date question.") + + response = self.client.get(reverse("polls:index")) + self.assertQuerySetEqual( + response.context["latest_question_list"], + [question], + ) + + def test_is_published_with_past_question(self): + """ + is_published() should return True for questions whose pub_date is in the + past. + """ + past_date = timezone.now() - datetime.timedelta(days=1) + past_question = Question(pub_date=past_date) + self.assertIs(past_question.is_published(), True) + + def test_can_vote_with_question_not_ended(self): + """ + can_vote() should return True for questions that are published and have not + ended. + """ + pub_date = timezone.now() - datetime.timedelta(hours=1) + end_date = timezone.now() + datetime.timedelta(hours=1) + question = Question(pub_date=pub_date, end_date=end_date) + self.assertIs(question.can_vote(), True) + + def test_can_vote_with_question_ended(self): + """ + can_vote() should return False for questions that are published but have + ended. + """ + pub_date = timezone.now() - datetime.timedelta(hours=2) + end_date = timezone.now() - datetime.timedelta(hours=1) + question = Question(pub_date=pub_date, end_date=end_date) + self.assertIs(question.can_vote(), False) + + def test_can_vote_with_question_no_end_date(self): + """ + can_vote() should return True for questions that are published and have no + specified end date. + """ + pub_date = timezone.now() - datetime.timedelta(hours=1) + question = Question(pub_date=pub_date, end_date=None) + self.assertIs(question.can_vote(), True) + + def test_can_vote_with_question_ending_in_future(self): + """ + can_vote() should return True for questions that are published and + the current time is within the allowed voting period. + """ + pub_date = timezone.now() - datetime.timedelta(hours=1) + end_date = timezone.now() + datetime.timedelta(hours=2) + question = Question(pub_date=pub_date, end_date=end_date) + self.assertIs(question.can_vote(), True) \ No newline at end of file diff --git a/polls/tests/test_vote_views.py b/polls/tests/test_vote_views.py new file mode 100644 index 0000000..393650e --- /dev/null +++ b/polls/tests/test_vote_views.py @@ -0,0 +1,86 @@ +from django.test import TestCase, Client +from django.urls import reverse +from django.contrib.auth.models import User + +from .base import create_question +from ..models import Vote, Choice + +class VoteViewTest(TestCase): + @classmethod + def setUpTestData(cls): + cls.question = create_question(question_text="Test Question", day=0) + cls.choice1 = Choice.objects.create(question=cls.question, choice_text="Test Choice 1") + cls.choice2 = Choice.objects.create(question=cls.question, choice_text="Test Choice 2") + cls.user = User.objects.create_user(username="test_user_123", password="aaa123321aaa") + cls.client = Client() + + def test_vote_with_valid_choice(self): + """ + Test the vote view with a valid choice selection. + """ + self.client.login(username=self.user.username, password="aaa123321aaa") + + response = self.client.post(reverse("polls:vote", args=(self.question.id,)), + {'choice' : self.choice1.id}) + + self.assertRedirects(response, reverse("polls:results", args=(self.question.id,))) + self.assertTrue(Vote.objects.filter(user=self.user, question=self.question).exists()) + + def test_vote_with_invalid_choice(self): + """ + Test the vote view with an invalid choice selection. + """ + self.client.login(username=self.user.username, password="aaa123321aaa") + + response = self.client.post(reverse("polls:vote", args=(self.question.id,)), + {'choice' : 1000}) + + self.assertRedirects(response, reverse('polls:detail', args=(self.question.id,))) + + def test_vote_without_login(self): + """ + Test the vote view when the user is not logged in. + """ + response = self.client.post(reverse("polls:vote", args=(self.question.id,)), + {'choice' : self.choice1}) + + self.assertRedirects(response, "/accounts/login/?next=/polls/1/vote/") + + def test_vote_voting_not_allowed(self): + """ + Test the vote view when voting is not allowed for the question. + """ + self.client.login(username=self.user.username, password="aaa123321aaa") + + self.question_2 = create_question(question_text="Test not allow", day=10) + self.choice_2 = Choice.objects.create(question=self.question_2, choice_text="Test Choice 2_2") + + response = self.client.post(reverse("polls:vote", args=(self.question_2.id,)), + {"choice" : self.choice_2.id}) + + self.assertRedirects(response, reverse('polls:index')) + + def test_vote_with_no_post_data(self): + """ + Test the vote view when vote with no post data. + """ + self.client.login(username=self.user.username, password="aaa123321aaa") + + response = self.client.post(reverse("polls:vote", args=(self.question.id,))) + + self.assertRedirects(response, reverse('polls:detail', args=(self.question.id,))) + + def test_update_vote_when_vote_on_new_choice(self): + """ + Test the vote when same user vote on same question but change the choice. + """ + self.client.login(username=self.user.username, password="aaa123321aaa") + + response_1 = self.client.post(reverse("polls:vote", args=(self.question.id,)), {"choice": self.choice1.id}) + self.assertRedirects(response_1, reverse('polls:results', args=(self.question.id,))) + + response_2 = self.client.post(reverse("polls:vote", args=(self.question.id,)), {"choice": self.choice2.id}) + self.assertRedirects(response_2, reverse('polls:results', args=(self.question.id,))) + + self.assertFalse(Vote.objects.filter(user=self.user, question=self.question, choice=self.choice1).exists()) + self.assertTrue(Vote.objects.filter(user=self.user, question=self.question, choice=self.choice2).exists()) \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index 3cd85c0..bcdf518 100644 --- a/polls/views.py +++ b/polls/views.py @@ -25,7 +25,7 @@ class IndexView(generic.ListView): """ now = timezone.now() return Question.objects.filter( - Q(pub_date__lte=now) & (Q(end_date__gte=now) | Q(end_date=None)) + Q(pub_date__lte=now) & ((Q(end_date__gte=now) | Q(end_date=None))) ).order_by("-pub_date") @@ -42,7 +42,10 @@ class DetailView(LoginRequiredMixin, generic.DetailView): """ Excludes any questions that aren't published yet. """ - return Question.objects.filter(pub_date__lte=timezone.now()) + now = timezone.now() + return Question.objects.filter( + Q(pub_date__lte=now) & (Q(end_date__gte=now) | Q(end_date=None)) + ).order_by("-pub_date") def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -88,6 +91,7 @@ class SignUpView(generic.CreateView): success_url = reverse_lazy('login') template_name = 'registration/signup.html' + @login_required def vote(request, question_id): """ @@ -95,29 +99,31 @@ def vote(request, question_id): in a specific question_id. """ question = get_object_or_404(Question, pk=question_id) - try: - selected_choice = question.choice_set.get(pk=request.POST["choice"]) - except (KeyError, Choice.DoesNotExist): - messages.error(request, "You didn't select a choice.") - return render(request, "polls/detail.html", {"question": question}) - else: + if request.method == "POST": + try: + selected_choice = question.choice_set.get(pk=request.POST["choice"]) + except (KeyError, Choice.DoesNotExist): + messages.error(request, "You didn't select a choice.") + return redirect("polls:detail", question_id) + if question.can_vote(): - if request.method == "POST" and "vote-button" in request.POST: - if Vote.objects.filter(user=request.user, question=question).exists(): - old_vote = question.vote_set.get(user=request.user) - old_vote.choice = selected_choice - old_vote.save() + # ! Return 1. object element 2. boolean status of creation + vote, created = Vote.objects.update_or_create( + user=request.user, + question=question, + defaults={'choice' : selected_choice} + ) - messages.success(request, "You vote successfully🥳") - return HttpResponseRedirect(reverse("polls:results", args=(question.id,))) - else: - Vote.objects.create(choice=selected_choice, user=request.user, question=question).save() - messages.success(request, "You vote successfully🥳") - return HttpResponseRedirect(reverse("polls:results", args=(question.id,))) + if created: + messages.success(request, "You voted successfully🥳") else: - messages.error(request, "You cannot vote by typing the URL.") - return render(request, "polls/detail.html", {"question": question}) + messages.success(request, "You updated your vote🥳") + + return redirect("polls:results", question_id) else: - messages.error(request, "You can not vote on this question.") - return HttpResponseRedirect(reverse("polls:index")) \ No newline at end of file + messages.error(request, "You cannot vote on this question.") + return redirect("polls:index") + else: + messages.error(request, "Invalid request method.") + return redirect("polls:index") \ No newline at end of file