0

So here is my view.py. In my models.py I have 2 models Question and a Choice the choice is connected to Question model. I am able to access the admin page.

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse, Http404
from django.template import loader
from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)


def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})


def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)


def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

This is my model for my Django project.

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

and here is the error I am getting this error in visual studio code in the problems page in the web when I run it I get the error Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name. :

Here is my urls.py

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

Here is my Index.html

{% if latest_question_list %}
<ul>
  {% for question in latest_question_list %}
  <li>
    <a href="{% url 'detail' question.id %}">{{ question.question_text }}</a>
  </li>
  {% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

and here is my detail.html

<h1>{{ question.question_text }}</h1>
<ul>
  {% for choice in question.choice_set.all %}
  <li>{{ choice.choice_text }}</li>
  {% endfor %}
</ul>

Class 'Question' has no 'objects' member

  • Are you sure there is no `Question` function/class/... in your `views.py` such that `Questions` thus refers to the wrong item? – Willem Van Onsem Mar 24 '20 at 11:15
  • 2
    Where are you getting that error? If it's from your code editor, then you can ignore it. If it appears when you run `manage.py runserver`, then please show the full `views.py` including the imports. – Alasdair Mar 24 '20 at 11:21
  • Okay so I am using visual studio code and in there I get a squiggly line saying "ss 'Question' has no 'objects' member" I can run the server but I get a error like "Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name. " –  Mar 24 '20 at 11:45
  • If you want to fix the warning from vscode, then [this question](https://stackoverflow.com/questions/45135263/class-has-no-objects-member) might help. For help with the `ReverseMatchError`, you need to show your `urls.py` and the template where you use `{% url 'detail' ... %}`. – Alasdair Mar 24 '20 at 11:57

1 Answers1

0

Your url name needs the app namespace, polls.

{% url 'polls:detail' question.id %}
schillingt
  • 13,493
  • 2
  • 32
  • 34