【问题标题】:DJANGO sending user trought href and HttpResponse errorDJANGO 发送用户 trought href 和 HttpResponse 错误
【发布时间】:2015-02-15 00:56:47
【问题描述】:

所以我试图在 HTML 页面中通过 url /Scan/{id} 发送参数:

<a href="/scan/{{c.id}}">
<button type="button" class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> Scan</button>
</a>

url.py:

url(r'^scan/(?P<idcmp>\w{0,50})/$', 'projman.views.scanning', name="scan"),

view.py:

def scanning(request, idcmp):
    context = init()

点击a href后的错误

视图 projman.views.scanning 未返回 HttpResponse 对象。它返回 None 。

我在我网站的其他部分有相同的代码,它可以工作。我不知道发生了什么。

【问题讨论】:

  • scanning 视图的其余部分在哪里?
  • context = init() tmp_component = component.objects.filter(pk=idcmp)[0] print tmp_component print tmp_component.scanned project(request, tmp_component.project.tla) 仅用于测试目的。我不认为问题出在功能上
  • 当然在函数里面。错误消息很清楚:您没有返回 HttpResponse。而且,确实,您不会从函数中返回任何内容。还有哪里有问题?

标签: python django href


【解决方案1】:
def scanning(request, idcmp):
    context = init()
    return HttpResponse('ok')

但只需解决您的问题,也许您应该添加一些尝试..except。并且不清楚 idcmp 的用途。

关于"/scan/{{c.id}}" 的一个问题:c 是什么?通常id 是整数,所以你的网址应该是

url(r'^scan/(?P<idcmp>\d+)/$', 'projman.views.scanning', name="scan"),

【讨论】:

  • 如果我想发送到另一个函数会发生什么?按下按钮 -> 扫描 -> SHowprojects def 扫描(request, idcmp): context = init() tmp_component = component.objects.filter(pk=idcmp)[0] print tmp_component print tmp_component.project.tla project(request, tmp_component.项目.tla)
  • 另一个功能? django 视图不是为您想要的而构建的。他们必须返回一个HttpResponse 对象。
猜你喜欢
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
相关资源
最近更新 更多