如何解决sql查询在django中不起作用:“ django中的格式字符串错误的参数不足”
我在MySQL中编写了一个SQL查询,它可以正常运行,但是当我在Django中使用此查询时,它运行时会出错。
我删除了数据库中“标题”的空间。当用户在搜索字段中输入内容时,我还将删除用户文本中的空格,然后开始在数据库中搜索用户文本中是否包含标题。
型号:
# Business (کسب و کار) Model
class Business(models.Model):
title = models.CharField(verbose_name = 'عنوان',max_length = 255)
slug = models.SlugField(verbose_name = 'شناسه',unique = True)
FK_CEO = models.ForeignKey(User,verbose_name = 'admin',related_name = 'business_admin_user',on_delete = models.SET_NULL,null = True)
description = models.TextField(verbose_name = 'توضیحات',blank = True)
createdate = models.DateTimeField(verbose_name = 'تاریخ ثبت',auto_now_add = True)
state = models.CharField(verbose_name = 'استان',max_length = 50,blank = True)
city = models.CharField(verbose_name = 'شهر',blank = True)
address = models.TextField(verbose_name = 'آدرس',blank = True)
FK_Point = models.ManyToManyField('Point',verbose_name = 'امتیازات',related_name = 'business_point',blank = True)
FK_Comment = models.ManyToManyField('Comment',verbose_name = 'نظرات',related_name = 'business_comment',blank = True)
phone = models.CharField(verbose_name = 'شماره تماس',max_length = 11,blank = True)
support_number = models.CharField(verbose_name = 'شماره تماس پشتیبانی',blank = True)
profile = models.ImageField(verbose_name = 'عکس پروفایل',upload_to = PathAndRename('media/images/business/'),default = 'static/img/joblogo.png',null = True)
categories = models.FileField(verbose_name = 'دسته بندی ها',upload_to = 'media/json/business/',null = True,blank = True)
FK_Activity = models.ForeignKey(Activity,verbose_name = 'زمینه فعالیت',related_name = 'business_activity',null = True)
FK_BusinessPartne = models.ManyToManyField('Business_Partner',verbose_name = 'شرکای تجاری',related_name = 'business_partners',blank = True)
FK_Album = models.ManyToManyField('Album',verbose_name = 'آلبوم ها',related_name = 'business_albums',blank = True)
FK_Product = models.ManyToManyField('Product',verbose_name = 'محصولات',related_name = 'business_products',blank = True)
FK_Service = models.ManyToManyField('Service',verbose_name = 'خدمات',related_name = 'business_services',blank = True)
FK_Tag = models.ManyToManyField(Tag,verbose_name = 'تگ ها',related_name = 'business_tags',blank = True)
PUBLIC_STATUS = (
(True,'شخصی'),(False,'عمومی'),)
public = models.BooleanField(verbose_name = 'وضعیت نمایش',choices = PUBLIC_STATUS,default = False)
PUBLISH_STATUS = (
(True,'منتشر شده'),'عدم انتشار'),)
publish = models.BooleanField(verbose_name = 'وضعیت انتشار',choices = PUBLISH_STATUS,default = False)
ORIGINAL_STATUS = (
(True,'اصیل'),'غیر اصیل'),)
original = models.BooleanField(verbose_name = 'وضعیت اصالت',choices = ORIGINAL_STATUS,default = False)
creator = models.CharField(verbose_name = 'سازنده',max_length = 255,null = True)
查看:
def search_page(request,text = None):
# set session
request.session['next'] = request.path
# check text word
this = None
if text != None:
text = text.replace("-"," ")
search_word = '%' + text + '%'
sql_query = 'SELECT * FROM main_business WHERE REPLACE(`title`," ","") LIKE {}'.format(search_word)
this = Business.objects.raw(str(sql_query))
# this = Business.objects.filter(description__search = text)
print(len(this.columns))
for item in this:
print(item.id)
context = {
'ThisBusiness':this,'SearchWord':text,}
return render(request,'main/pages/search.html',context)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。