如何在Django模板中声明动态链接?

时间:2018-09-11 20:42:41

标签: python django django-templates django-views

我要在另一个列表中创建一个列表。例如home / 4/5 /等 另外,如果我想从路径更改名称,则会出现错误,提示没有反向。

path('findstudent/', views.FindStudent.as_view(), name='findstudent'),
path('findstudent/<int:pk>/', views.FindStudentdetail.as_view(), name='findstudent'),
path('findstudent/<int:pk>/<int:pk_alt>/', views.FindStudentresult.as_view(), name='findstudent'),


@method_decorator(login_required, name='dispatch')
class FindStudent(ListView):
    template_name = 'Dashboard/findStudent.html'
    model = Student
    fields = ['sbtc']

    def get_queryset(self):
        batch = Student.objects.values_list('sbtc').distinct()
        return batch

@method_decorator(login_required, name='dispatch')
class FindStudentdetail(ListView):
    template_name = 'Dashboard/findStudentdetail.html'
    model = Student
    fields = ['all']

    def get_queryset(self):
        student = Student.objects.filter(sbtc=self.kwargs['pk'])
        return student

@method_decorator(login_required, name='dispatch')
class FindStudentresult(ListView):
    template_name = 'Dashboard/findStudentresult.html'
    model = Result
    fields = ['all']

    def get_queryset(self):
        result1 = Result.objects.select_related('Student')
        result = Result.objects.filter(id=self.kwargs['pk_alt'])
        return result

问题在这里: 如何在模板中定义路径/ URL!

    <div class="form-group">
                      {% for student in object_list %}
                <li><a href="{% url 

    **'Dashboard:findstudent'**

 %}">{{student.sroll}} {{student.snam}}</a></li>
                {% endfor %}
                  </div>

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码编辑网址:

小心翼翼地用相同的名称调用所有网址!

path('findstudent/', views.FindStudent.as_view(), name='findstudent'),
path(r'^findstudent/(?P<int:pk>)/$', views.FindStudentdetail.as_view(), name='findstudent'),
path(r'^findstudent/(?P<int:pk>)/(?P<int:pk_alt>)/$', views.FindStudentresult.as_view(), name='findstudentTemplate'),


{% for student in object_list %}
            <li><a href="{% url 'findstudentTemplate' student.sroll student.snam %}"></a></li>
            {% endfor %}