I have an app were I'm trying to make a course with modules in it, and inside the course page I want to link a page with the modules of the course in it but I get "TypeError at /modules modules_page() missing 1 required positional argument: 'id'".
Sorry for my bad explaining. I appreciate every answer, thanks in advance!
Models.py
class Course(models.Model):
title = models.CharField(max_length=30)
description = models.TextField(null=True, max_length=150)
#completion_time = models.CharField(max_length=30)
course_image = models.ImageField(blank=True, null=True, upload_to="images/")
watching = models.ManyToManyField(User, blank=True, related_name="watchlist")
category = models.ForeignKey(Category, on_delete=models.CASCADE ,related_name="listing_category")
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name="author")
ended = models.BooleanField(default=False)
creation_date = models.DateTimeField(default=timezone.now)
class Module(models.Model):
title = models.CharField(max_length=255, null=True)
description = models.TextField(blank=True, max_length=2550, null=True)
module_image = models.ImageField(blank=True, null=True, upload_to="images/")
creation_date = models.DateTimeField(default=timezone.now)
module_author = models.ForeignKey(User, on_delete=models.PROTECT, related_name="module_author")
deadlined = models.BooleanField(default=False)
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name="modules", null=True)
Views.py
def courses_page(request):
courses = Course.objects.all()
return render(request, "auctions/index.html", {
"courses": courses
})
# THIS FUNCTION IS NOT WORKING
def modules_page(request, id):
if not request.user.is_authenticated:
return render(request, "auctions/login.html")
course = Course.objects.get(id=id)
ended = Course.ended
return render(request, "auctions/modules_page.html", {
"course": course,
"course_ended": "Course has ended.",
"commentform": CommentForm()
})
Urls.py
path("", views.index, name="index"),
path("auction/course/<str:id>", views.course, name="course"),
path("modules", views.modules_page, name="modules_page"),
Template.html:
<!-- Modules -->
<h3><span >{{ course.modules.count }}</span><a href="{% url 'modules_page' course.id %}">Modules:</a></h3>
{% for module in course.modules.all %}
<div >
<div >
<h4 ><a href="{% url 'module' module.id %}">{{ module.title }}</a></h4>
<h6 >{{ module.creation_date }}</h6>
<ul>
<h6>{{ module.description }}</h6>
{% endfor %}
CodePudding user response:
First thing instead of rendering login page from module function use login_required decorator from as follow
from django.contrib.auth.decorators import login_required
@login_required
def modules_page(request, id):
# your code here
Your module function expects course id
your url patterns should look like this
path("modules/<int:id>/", views.modules_page, name="modules_page"),
In course list page; In the for loop section where you are getting each course; The link tag that takes to module page should be
<a href={% url 'modules_page' id=course.id %}>Module Link</a>