Hello, I am having a very similar error. Everything works perfectly before switching to Generic Views but after the switch I get the error:
NoReverseMatch at /stocks/11/
Reverse for ‘vote’ with arguments ‘(’‘,)’ not found. 1 pattern(s) tried: [‘stocks/(?P<tn_id>[0-9]+)/vote/\Z’]
I have tried retyping everything that changes from before generic till after but still no luck. I have changed the model names and polls-> stocks to aid my understanding, but have tried to point out the changes. Please let me know if a mapping isn’t clear. Here is the supporting code:
#models
#in place of the Question Model in the tutorial
> class Weighbridge(models.Model):
> weighdate = models.DateTimeField(default=now) #like pubdate
> tn = models.AutoField(primary_key=True)
> supplier = models.ForeignKey(Supplier,on_delete=models.PROTECT) #like question_text
#in place of the Choice model in the tutorial
class LastLoad(models.Model):
tn = models.ForeignKey(Weighbridge, on_delete=models.CASCADE) #like question in the tutorial
commodity_last = models.ForeignKey(Commodity, on_delete=models.PROTECT) #like choice_text in the tutorial
votes = models.IntegerField(default=0) #as the tuorial
#urls
app_name = 'stocks'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
#path("<int:tn_id>/", views.detail, name='detail'),
#path("<int:tn_id>/results/", views.results, name='results'),
path("<int:pk>/", views.DetailView.as_view(), name="detail"),
path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
path("<int:tn_id>/vote/", views.vote, name="vote"),
]
from django.shortcuts import render, get_object_or_404
from .models import Weighbridge, LastLoad
from django.http import HttpResponse, HttpResponseRedirect
from django.views import generic
from django.urls import reverse
class IndexView(generic.ListView):
template_name = "stocks/index.html"
context_object_name = "latest_weighbridge_list" #like latest_question_list
def get_queryset(self):
#Return the last five tn
return Weighbridge.objects.order_by('weighdate')[5:]
class DetailView(generic.DetailView):
model = Weighbridge
template_name = "stocks/detail.html"
class ResultsView(generic.DetailView):
model = Weighbridge
template_name = 'stocks/results.html'
def vote(request, tn_id):
ticket = get_object_or_404(Weighbridge, pk=tn_id)
selected_choice = ticket.lastload_set.get(pk=request.POST["lastload"])
try:
selected_choice = ticket.lastload_set.get(pk=request.POST["lastload"])
print(selected_choice.commodity_last) #just printing for my understanding
except (KeyError, LastLoad.DoesNotExist):
#Redisplay the ticket voting form.
return render(request,
"stocks/detail.html",
{"ticket": ticket, "error_message": "You didn't select anything,"},
)
else:
selected_choice.votes += 1
selected_choice.save()
#Always return an HttpResponseRedirect after successfully dealing
#with POST data. This precents being posted twice if user hits Back button.
return HttpResponseRedirect(reverse("stocks:results", args = (ticket.tn,)))
Is the error caused by something trivial or is it because the models are not exactly the same as the ones in the tutorial - how do I fix it? I don’t fully understand Generic Views and have tried with the source material (and this) referenced in other questions but not really sure what I’m looking for.
Thanks
Georgia