Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django当我试图搜索一个表单时,它的值得到u';_Django_Unicode_Get - Fatal编程技术网

Django当我试图搜索一个表单时,它的值得到u';

Django当我试图搜索一个表单时,它的值得到u';,django,unicode,get,Django,Unicode,Get,我正试图学习搜索教程,但我遇到了一个错误,显示我搜索的是u'foo',而不是foo。如果我只打印带有'%s'%q的消息,它只显示foo def search(request): q = request.GET.get("q",None) if q: message = '%s' % q message = Envio.objects.get(destinatario=message) else: message = 'Empty'

我正试图学习搜索教程,但我遇到了一个错误,显示我搜索的是
u'foo'
,而不是
foo
。如果我只打印带有
'%s'%q
的消息,它只显示
foo

def search(request):
   q = request.GET.get("q",None)
   if q:
       message = '%s' % q
       message = Envio.objects.get(destinatario=message)
   else:
      message = 'Empty'
    return HttpResponse(message)

def search_form(request):
     return render_to_response('envios/search_form.html')
错误:

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  100.                     response = callback(request, *callback_args,     **callback_kwargs)
File "C:\mysite\envios\views.py" in search
  35.         message = Envio.objects.get(destinatario=message)
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in get
  132.         return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in get
   347.                     % self.model._meta.object_name)

   Exception Type: DoesNotExist at /envios/search/
   Exception Value: Envio matching query does not exist.
除此之外:

Request information

 GET
 Variable     Value
 q             u'102'
u“”只表示字符串以unicode显示。django中的默认编码是unicode。不要为u“”操心太多,实际结果将始终是单引号之间的字符串

有关更多信息,请查看此处

所以,如果查询中的类型不匹配,您可能会得到错误。所以不是

message = '%s' % q 
试一试


如适用。

请显示错误。错误发生在哪里?请注意,如果
打印“%r”%q
,您将看到unicode字符串的实际表示形式。@Jaime-destinatario是Envio模型的字段吗?当我使用shell执行objetcs.get(id=something)时,它可以工作。@Jaime-是destinatario字符串类型或整数类型。如果查询中的类型不匹配,则可能会出现所得到的错误。因此,如果适用,请尝试使用
message=str(q)
message=int(q)
代替
message='%s'%q
message = str(q) 
message = int(q)