我在一门课程的一个表中有多个课程。当用户从课程中查看课程时,还需要能够访问上一课程和下一课程的链接。
models.py
class Course(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
created_dt = models.DateTimeField(auto_now_add=True)
enrollment = models.ManyToManyField(get_user_model())
def __str__(self):
return self.title
class Step(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
code_sample_1 = models.TextField(blank=True)
code_sample_2 = models.TextField(blank=True)
code_sample_3 = models.TextField(blank=True)
code_sample_4 = models.TextField(default=" ", blank=True)
project_1 = models.TextField(blank=True)
project_2 = models.TextField(blank=True)
project_3 = models.TextField(blank=True)
video_url = models.CharField(max_length=255,blank=True)
resources = models.TextField(default=" ",blank=True)
file_url = models.CharField(max_length=255,blank=True)
order = models.IntegerField(default=0)
course = models.ForeignKey(Course,on_delete=models.CASCADE)
class Meta:
ordering = ['order',]
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('course_steps', args=[str(self.id)])
def get_next_by_title(self):
try:
return self._get_next_or_previous_by_FIELD('title', is_next=True)
except Step.DoesNotExist:
return None
def get_previous_by_title(self):
try:
return self._get_next_or_previous_by_FIELD('title', is_next=False)
except Step.DoesNotExist:
return None
模板中的代码
<a href="{{ step.get_previous_by_title.get_absolute_url }}">{{ step.get_previous_by_title.title }}</a>
url.py
from django.urls import path,re_path
from . import views
urlpatterns = [
path('', views.course_list,name="courses"),
re_path(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail,name="course_step_detail"),
re_path(r'(?P<pk>\d+)/$', views.course_detail,name="course_steps"),
]
按要求提供了“完全错误回溯”。希望这将有助于发现问题。
Traceback (most recent call last):
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/PycharmProjects/Portal/PenroseLearningApp/courses/views.py", line 33, in step_detail
return render(request,"courses/step_detail.html",{'step':step})
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 987, in render
output = self.filter_expression.resolve(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 671, in resolve
obj = self.var.resolve(context)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/template/base.py", line 796, in resolve
value = self._resolve_lookup(context)
File "/Users/saumyanair/PycharmProjects/CodingPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 858, in _resolve_lookup
current = current()
File "/Users/PycharmProjects/Portal/PenroseLearningApp/courses/models.py", line 51, in get_previous_by_title
return self._get_next_or_previous_by_FIELD('title', is_next=False)
File "/Users/PycharmProjects/Portal/venv/lib/python3.7/site-packages/django/db/models/base.py", line 933, in _get_next_or_previous_by_FIELD
param = getattr(self, field.attname)
AttributeError: 'str' object has no attribute 'attname'