【问题标题】:Python/ Django- copying buttons from one HTML page to anotherPython / Django-将按钮从一个HTML页面复制到另一个
【发布时间】:2016-11-17 17:36:53
【问题描述】:

我刚刚接手了一些项目管理软件的开发,已经用 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 %}

但是当我现在尝试在浏览器中查看此页面时,我收到一个错误页面,上面写着:

NoReverseMatch 在 /costing/5915/variations/

我不确定为什么会出现此错误...我是否需要引用对我在 HTML 文件中其他地方复制的代码中使用的视图的任何调用?这两个 HTML 文件都在同一个应用程序中,所以共享同一个 models.py 文件 - 所以我原以为他们都可以使用在这个应用程序中定义的所有 modelsviews

是这样吗?如果是这样,为什么我会在变体页面上收到此错误?

【问题讨论】:

  • 如果您正在复制和粘贴代码,那么您已经做错了。
  • 但是,如果您需要真正的帮助,您需要发布 full 错误和回溯。

标签: python html django


【解决方案1】:

根据您所说的,我认为问题是您在上下文变量中没有“预算”值。我认为使用第一个模板的视图在其上下文中发送“预算”。 但第二个观点没有。这就是为什么当您尝试在第二个模板中获取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

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 2018-03-31
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2014-10-27
    • 2019-11-29
    • 2019-12-03
    • 2016-12-30
    相关资源
    最近更新 更多