我刚接手开发了一些用Python / Django编写的项目管理软件 - 之前没有使用过Python或Django ......
其中一个网页上显示了一些按钮,可以在应用程序的另一个页面上显示。我可以看到这些按钮在budget.html
中使用以下代码定义:
{% block page_options %}
<a class="button m-r-md" href="{% url 'costing:export_csv' budget.id %}">Export to Excel</a>
<a class="button m-r-md" href="{% url 'costing:schedule_of_works_post_dep' budget.id %}" target="_blank">Schedule of works</a>
<a class="button m-r-md" href="?pdf=1" target="_blank">PDF</a>
<input data-view-url="{% url 'costing:combined_budget' project.id %}?search=" type="text" id="item_search" placeholder="Item search" />
{% endblock page_options %}
我希望能够使用它们的另一个页面 - variations.html
在{%block page_options %}
块中已经包含以下代码:
{% block page_options %}
<button class="button modalBtn" name="variation">+ Add variation</button>
<a class="button" href="{% url 'costing:add_omit_builder' project.id %}">+ Add group</a>
<a class="button" id="scas" data-view-url="{% url 'costing:preview_scas' project.budget_overview.version.id %}" href="{% url 'costing:scas_2_variations' project.budget_overview.version.id %}">+ Standard cost assumptions</a>
<!--ERF(17/11/2016 @ 1700) Add buttons to export adds/ omits table to excel -->
{% endblock page_options %}
所以我尝试将第一页的代码复制并粘贴到第二页的这个块中:
{% block page_options %}
<button class="button modalBtn" name="variation">+ Add variation</button>
<a class="button" href="{% url 'costing:add_omit_builder' project.id %}">+ Add group</a>
<a class="button" id="scas" data-view-url="{% url 'costing:preview_scas' project.budget_overview.version.id %}" href="{% url 'costing:scas_2_variations' project.budget_overview.version.id %}">+ Standard cost assumptions</a>
<!--ERF(17/11/2016 @ 1700) Add buttons to export adds/ omits table to excel -->
<a class="button m-r-md" href="{% url 'costing:export_csv' budget.id %}">Export to Excel</a>
<a class="button m-r-md" href="{% url 'costing:schedule_of_works_post_dep' budget.id %}" target="_blank">Schedule of works</a>
<a class="button m-r-md" href="?pdf=1" target="_blank">PDF</a>
<input data-view-url="{% url 'costing:combined_budget' project.id %}?search=" type="text" id="item_search" placeholder="Item search" />
{% endblock page_options %}
但是当我现在尝试在浏览器中查看此页面时,我会看到一个错误页面:
> / costing / 5915 / variants / 的NoReverseMatch
我不确定为什么我会收到此错误...我是否需要引用我在HTML文件中其他地方复制的代码中使用的视图的任何调用?两个HTML文件都在同一个应用程序中,因此共享相同的models.py
文件 - 所以我认为他们都可以使用所有models
&amp;此应用中定义了views
?
是这样的吗?如果是这样,为什么我在变体页面上收到此错误?
答案 0 :(得分:0)
基于你所说的我认为问题是你在上下文变量中没有“预算”值。我认为使用第一个模板的视图在其上下文中发送“预算”。 但第二种观点却没有。这就是为什么当您尝试在第二个模板中获取budget.id时会出现错误。
尝试修改视图中的上下文并为其添加“预算”变量。
def my_view(request, pk):
budget= get_object_or_404(Budget, pk=pk)
return render(request, 'budget.html', {'budget': budget})
或者,如果您使用的是基于类的视图,则应覆盖get_context_data方法:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['budget'] = self.budget
return context