【问题标题】:No data is sent with GET methodGET方法不发送数据
【发布时间】:2012-05-11 21:06:27
【问题描述】:

我正在尝试在用 Django 编写的简单搜索和显示应用程序中使用分页。

我已按照 Djangoproject 的 Pagination 教程进行操作,但没有数据发送到服务器。

我用pdb.set_trace()查看了代码的输出,GET字典是空的。

这是模板和URLs文件中的代码:

results.html:

<form method="GET" id="searchForm" action="/search/">
        <input type="text" id="billSearched" name="q_word">
        <input type="submit" value="{% trans "Look for" %}">
</form>

urls.py:

urlpatterns = patterns('',
    url(r'^$','ps.views.bills',name="bills"),
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^search/','ps.views.search',name="search"),)

以及与之配合使用的视图,search.py​​:

def search(request):
    import pdb
    pdb.set_trace()
    searchTerm = request.GET.get('q_word')
    if searchTerm == None:
        searchTerm = "test"
    found_bills = Bill.objects.filter(name__icontains = searchTerm)
    page = request.GET.get('page')
    paginator = Paginator(found_bills,25)
    try:
        current_page = paginator.page(page)
    except PageNotAnInteger:
        current_page = paginator.page(1)
    except (EmptyPage, InvalidPage):
        current_page = paginator.page(paginator.num_pages)
return render(request,'results.html',{"results":current_page,"term": searchTerm})

为什么没有数据发送?我也阅读了其他帖子,但那里的解决方案对我不起作用。他们建议使用request.GET.get('q_word')request.GET['q_word'] 访问GET 字典中的'q_word' 值,根据他们的回答应该可以,但对我来说不行。

我的错误在哪里?非常感谢您!

更新:

[02/May/2012 14:03:59] "GET / HTTP/1.1" 200 39694
Traceback (most recent call last):
  File "/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 284, in run
    self.finish_response()
  File "/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 324, in finish_response
    self.write(data)
  File "/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 403, in write
    self.send_headers()
  File "/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 467, in send_headers
    self.send_preamble()
  File "/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 385, in send_preamble
    'Date: %s\r\n' % http_date()
  File "/usr/lib/python2.7/socket.py", line 324, in write
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 59087)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 570, in __init__
    BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 641, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 694, in finish
    self.wfile.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

【问题讨论】:

  • search 页面上的 URL 中是否包含 ?q_word=...
  • 确保在提交表单后 URL 以 ?q_word=search_term 结尾
  • 不,没有任何内容附加到 URL。我最近制作了一个适用于 Udacity 的教程,但在这里它不起作用。
  • @reos URL 必须以?q_word=search_term结尾
  • @JosvicZammit 你建议我修改URLs 文件吗?

标签: html django forms search pagination


【解决方案1】:

问题是提交时查询参数没有附加到 URL 的末尾。我建议您对表单使用以下语法:

<form id="searchForm" method="GET" action="/search/">
<fieldset>
<input type="text" id="billSearched" name="q_word">
<br />
<input type="submit" value="{% trans "Look for" %}">
</fieldset>
</form>

当您提交表单时,这应该使您的 URL 以 ?q_word=search_term 结尾。

urls.py 看起来不错。

【讨论】:

  • 错误:Cannot use None as a query value。似乎数据仍然没有通过。顺便说一句,网址是http://127.0.0.1:8000/search/,没有附加任何内容。
  • @reos 问题是QueryString 没有附加到您的URL。如果是,您将在视图的request 对象中收到它。表单语法有问题,trans 标签是否正常工作?如果删除trans 会怎样?只是试验...
  • 没错,我可以看到,但我想弄清楚这是否是代码的问题。我正在尝试缩小范围..
  • @reos 表格有问题;提交时检查您的 HTTP 响应。它是否被重写(随后丢失了查询字符串)?
  • 如何查看 HTTP 是否被重写?
猜你喜欢
  • 2018-07-18
  • 2011-07-28
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 2016-08-29
  • 2019-06-03
  • 1970-01-01
  • 2010-12-23
相关资源
最近更新 更多