没有数据使用GET方法发送 [英] No data is sent with GET method

查看:120
本文介绍了没有数据使用GET方法发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我按照 Pagination from Djangoproject,但没有数据被发送到服务器。



我使用 pdb.set_trace()来查看代码的输出,并且 GET dictionary是空的。



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



results.html:

 < form method =GETid =searchFormaction =/ search /> 
< input type =textid =billSearchedname =q_word>
< input type =submitvalue ={%trans寻找%}>
< / 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​​: $ b $ pre $ def search(请求):
导入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)
PageNotAnInteger :
current_page = paginator.page(1)
除了(EmptyPage,InvalidPage):
current_page = paginator.page(paginator .nu​​m_pages)
return render(request,'results.html',{results:current_page,term:searchTerm})

为什么没有数据被发送?我也阅读过其他文章,但那里的解决方案并不适合我。他们建议通过 request.GET.get('q_word')或者使用 request.GET来访问GET字典中的'q_word' 'q_word'] 和他们的答案,它应该工作,但对我来说它不是。



我的错误在哪里?
$ b

更新:

  [02 / May / 2012 14:03:59]GET / HTTP / 1.1200 39694 
Traceback(最近一次调用最后一次):
File/ home / radu / projects /psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py,第284行,运行
self.finish_response()
文件/ home / radu / projects / psdemo / .venv / local / lib / python2.7 / site-packages / django / core / servers / basehttp.py,第324行,在finish_response
self.write(data)
文件/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py,第403行,写入
self .send_headers()
文件/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py,第467行,在send_headers
self.send_preamble()
文件/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py,第385行,在send_preamble
'D ate:%s \r\\\
'%http_date()

self.flush()$中写入/usr/lib/python2.7/socket.py,第324行b $ b在flush
self._sock.sendall(view [write_offset:write_offset + buffer_size])
错误:[
]中的第303行显示/usr/lib/python2.7/socket.py Errno 32]损坏的管道

------------------------------------- ---
在处理来自('127.0.0.1',59087)
Traceback(最近调用最后一个)的请求期间发生异常:
文件/usr/lib/python2.7/ SocketServer.py,第284行,在_handle_request_noblock
self.process_request(request,client_address)
文件/usr/lib/python2.7/SocketServer.py,行310,in process_request
self.finish_request(request,client_address)
文件/usr/lib/python2.7/SocketServer.py,第323行,在finish_request
self.RequestHandlerClass(request,client_address,self)
文件/home/radu/projects/psdemo/.venv/local/lib/python2.7/site-packages/django/core/servers/basehttp.py ,第570行,在__init__
BaseHTTPRequestHandler .__ init __(self,* args,** kwargs)
文件/usr/lib/python2.7/SocketServer.py,行641,在__init__
self.finish()
文件/usr/lib/python2.7/SocketServer.py,行694,完成
self.wfile.flush()
文件/ usr / lib / python2.7 / socket.py,第303行,在flush
self._sock.sendall(view [write_offset:write_offset + buffer_size])
错误:[Errno 32] Broken pipe
----------------------------------------


解决方案

问题是,查询参数不会被追加到URL的末尾提交。我建议你在表单中使用这种语法:

 < form id =searchFormmethod =GETaction = /搜索/> 
< fieldset>
< input type =textid =billSearchedname =q_word>
< br />
< input type =submitvalue ={%trans寻找%}>
< / fieldset>
< / form>

这应该使您的网址以结尾?q_word = search_term

urls.py 看起来不错。


I'm trying to use Pagination in my simple search and display application written in Django.

I've followed the tutorial on Pagination from Djangoproject but there is no data being sent to the server.

I've used pdb.set_trace() to view the output of the code, and the GET dictionary is empty.

Here is the code in the template and the URLs file:

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"),)

and the view that works with this, 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})

Why is there no data being sent? I have read other posts as well and the solutions there didn't work for me. They suggested to access the 'q_word' value in the GET dictionary with either request.GET.get('q_word') or with request.GET['q_word'] and by their answers it should work, but for me it doesn't.

Where is my mistake? Thank you very much in advance!

UPDATE:

[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
----------------------------------------

解决方案

The problem is that query parameters are not being appended to the end of the URL when you submit. I suggest you use this syntax for the form:

<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>

This should make your URL end with ?q_word=search_term when you submit the form.

urls.py looks fine.

这篇关于没有数据使用GET方法发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆