关于上下文数据的NoReverseMatch

时间:2018-01-11 12:20:38

标签: django django-views

我正在尝试访问我的上下文数据但由于某种原因我收到错误。

  

使用关键字参数“{'pk1':''}'反转'applicant_register3'   找不到

.....................................

{% extends 'base.html' %}
{% load static %}
{% block body %}
<div class="container paddingtop80 marginbottom30">
  <div class="jumbotron greenback">
      Test
      <a href="{% url 'registration:applicant_register3' pk1=project.id %}" class="btn btn-success" role="button"><span class="glyphicon glyphicon-plus"></span>   Add Applicants</a>
  </div>
</div>

我正在尝试使用pk1 = project.id

我的观点应该允许我访问它..

class RecruitmentPage(generic.ListView):
    #import pdb; pdb.set_trace()
    template_name = "recruitment_index.html"
    model = Project 

    def get_object(self, queryset=None):
        return get_object_or_404(Project, id=self.kwargs['pk1'])

    def get_context_data(self, **kwargs):
         context = super(RecruitmentPage, self).get_context_data(**kwargs)
         return context

我猜想通过在通用视图中提供模型我可以访问它,但似乎我不能..

有人可以帮我一把吗? thx

1 个答案:

答案 0 :(得分:0)

您收到import tensorflow as tf NODE_OPS = ['Placeholder','Identity'] MODEL_FILE = '/path/to/frozen_inference_graph.pb' gf = tf.GraphDef() gf.ParseFromString(open(MODEL_FILE,'rb').read()) print([n.name + '=>' + n.op for n in gf.node if n.op in (NODE_OPS)]) 错误,因为NoRevereseMatch不在模板上下文中。

列表视图不会自动调用您的project方法。您可以使用get_object方法手动调用它:

get_context_data

现在,在您的模板中,您可以使用class RecruitmentPage(generic.ListView): #import pdb; pdb.set_trace() template_name = "recruitment_index.html" model = Project def get_object(self, queryset=None): return get_object_or_404(Project, id=self.kwargs['pk1']) def get_context_data(self, **kwargs): context = super(RecruitmentPage, self).get_context_data(**kwargs) context['current_project'] = self.get_object() return context 循环遍历所有项目的列表。

project_list

您可以使用{% for project in project_list %} {% endfor %}

get_object访问该项目
current_project