对于基数为10的int(),Django无效文字 [英] Django invalid literal for int() with base 10

查看:154
本文介绍了对于基数为10的int(),Django无效文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



用户通过文本进行搜索,并从下拉列表中添加一个城市字段。



查询如下:

 如果请求中的查询.GET并请求。 GET ['query']:
city = request.GET ['city']
q = request.GET ['query']
ct = Classified.objects.filter(name__icontains = q) .filter(city__exact = city)
return render(request,'template / search.html',{'ct':ct,'query':q})
/ pre>

分类模型如下:

  class分类(models.Model):
name = models.CharField(max_length = 256,unique = True)
email = models.CharField(max_length = 100)
category = models.ForeignKey )
phone_number = models.IntegerField(max_length = 10,default = 0)
address = models.CharField(max_length = 1000)
id = models.IntegerField(primary_key = True)
city = models.ForeignKey(Cities)
image = models.ImageField(blank = True,upload_to ='static / img /')

def __unicode __(self):
return自我名称

城市如下:

  class Cities(models.Model):
name = models.CharField(max_length = 256)

def __unicode __(self):
返回self.name

在搜索中,/ search /页面给我以下错误: p>

 基数为10的int()的无效字面值:'Searched-city'
pre>

我错过了什么?

解决方案

一个外键,您需要通过明确说明城市 __ name __ exact

  ct = Classified.objects.filter(name__icontains = q).filter(city__name__exact = city)


I'm working a simple search result page in Django.

The user searches by text and adds a city field from a dropdown.

The query is as follows:

if 'query' in request.GET and request.GET['query']:
    city = request.GET['city']
    q = request.GET['query']
    ct = Classified.objects.filter(name__icontains=q).filter(city__exact=city)
    return render(request, 'template/search.html', {'ct': ct, 'query': q})

The model for Classified is as follows:

class Classified(models.Model):
    name = models.CharField(max_length=256, unique=True)
    email = models.CharField(max_length=100)
    category = models.ForeignKey(Categories)
    phone_number = models.IntegerField(max_length=10, default=0)
    address = models.CharField(max_length=1000)
    id = models.IntegerField(primary_key=True)
    city = models.ForeignKey(Cities)
    image = models.ImageField(blank=True, upload_to='static/img/')

    def __unicode__(self):
        return self.name

Cities is as follows:

class Cities(models.Model):
    name = models.CharField(max_length=256)

    def __unicode__(self):
        return self.name

On search, the /search/ page gives me the following error:

invalid literal for int() with base 10: 'Searched-city'

What am I missing out on?

解决方案

City is a foreign key, you need to update your query to proxy through the relationship by explicitly saying city__name__exact

ct = Classified.objects.filter(name__icontains=q).filter(city__name__exact=city)

这篇关于对于基数为10的int(),Django无效文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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