Reputation: 53
I am stuck trying to figure out how to fix this error. I know what the error is referring to (path(quizzes)), but I don't understand how to fix it although I tried pk although i may not have done it right. Here is my url.py and models
urlpatterns = [
path('JSUMA/', include('JSUMA.urls')),
path('admin/', admin.site.urls),
path("register/", v.register, name="register"),
path('', include("django.contrib.auth.urls")),
path("contact/", v.contactview, name="contact"),
path("Quizzes/", v.QuizView.as_view(), name="quiz")
]
models.py
class User(models.Model):
first_name = models.CharField(max_length=25)
last_name = models.CharField(max_length=25)
#password = models.CharField(max_length=25)
email = models.EmailField(max_length=100)
class Quiz(models.Model):
name = models.CharField(max_length=200,primary_key=True)
NOQ = models.IntegerField(default=1)
class Meta:
verbose_name = "Quiz"
verbose_name_plural = "Quizzes"
def __str__(self):
return self.name
#number Of Questions
class Major(models.Model):
major = models.CharField(max_length=200)
majorData = models.IntegerField(default=0)
answer = models.ManyToManyField('Answer')
def __str__(self):
return self.major
class Question(models.Model):
question_text = models.CharField(max_length=400)
quiz = models.ForeignKey("Quiz", on_delete=models.CASCADE, null=True)
def __str__(self):
return self.question_text
class Answer(models.Model):
question = models.ForeignKey('Question', on_delete=models.CASCADE, null=True)
answer_text = models.CharField(max_length=200)
def __str__(self):
return self.answer_text
Upvotes: 0
Views: 44
Reputation: 476614
The QuizView
is a DetailView
, thus that means we need something to specify what item to display. By default Django will look for a pk
(type int
), or slug
(type slug
). You thus need to include this in the URL path:
urlpatterns = [
path('Quizzes/<int:pk>/', v.QuizView.as_view(), name='quiz')
]
you thus then view the item with primary key 14
for example with /Quizzes/14/
.
then the view thus looks like:
from django.views.generic.detail import DetailView
class QuizView(DetailView):
model = Quiz
in the template you can then access the related Question
s with:
{% for question in object.question_set.all %}
{{ question_text }}
{% endfor %}
In case you want to list all items, then you should use a ListView
:
from django.views.generic.list import ListView
class QuizView(ListView):
# …
then you do not specify a pk
/slug
, since then the ListView
will list all items, or perhaps a paginated variant.
Upvotes: 1