当我想在Django中实现搜索功能时有些错误

时间:2015-09-23 12:08:46

标签: python mysql django search

这是我的static int HashItems(params object[] items) { const int prime1 = 17; const int prime2 = 23; return items .Select(item => item != null ? item.GetHashCode() : 0) .Aggregate(prime1, (current, itemHash) => unchecked(current*prime2 + itemHash)); } (模型是由django从MySQL数据库中提取的):

models.py

这是我的from __future__ import unicode_literals from django.db import models class Cyshici(models.Model): id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase. field_1 = models.CharField(db_column='\u5b57\u6bb51', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_2 = models.CharField(db_column='\u5b57\u6bb52', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_3 = models.CharField(db_column='\u5b57\u6bb53', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_4 = models.CharField(db_column='\u5b57\u6bb54', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_5 = models.CharField(db_column='\u5b57\u6bb55', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_6 = models.CharField(db_column='\u5b57\u6bb56', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_7 = models.CharField(db_column='\u5b57\u6bb57', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_8 = models.CharField(db_column='\u5b57\u6bb58', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_9 = models.CharField(db_column='\u5b57\u6bb59', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_10 = models.CharField(db_column='\u5b57\u6bb510', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_11 = models.CharField(db_column='\u5b57\u6bb511', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_12 = models.CharField(db_column='\u5b57\u6bb512', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_13 = models.CharField(db_column='\u5b57\u6bb513', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_14 = models.CharField(db_column='\u5b57\u6bb514', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_15 = models.CharField(db_column='\u5b57\u6bb515', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_16 = models.CharField(db_column='\u5b57\u6bb516', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_17 = models.CharField(db_column='\u5b57\u6bb517', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_18 = models.CharField(db_column='\u5b57\u6bb518', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_19 = models.CharField(db_column='\u5b57\u6bb519', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_20 = models.CharField(db_column='\u5b57\u6bb520', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_21 = models.CharField(db_column='\u5b57\u6bb521', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_22 = models.CharField(db_column='\u5b57\u6bb522', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_23 = models.CharField(db_column='\u5b57\u6bb523', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_24 = models.CharField(db_column='\u5b57\u6bb524', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_25 = models.CharField(db_column='\u5b57\u6bb525', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_26 = models.CharField(db_column='\u5b57\u6bb526', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_27 = models.CharField(db_column='\u5b57\u6bb527', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. class Meta: managed = False db_table = 'cyshici' def __unicode__(self): return self.field_1 class Shici(models.Model): id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase. field_1 = models.CharField(db_column='\u5b57\u6bb51', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_2 = models.CharField(db_column='\u5b57\u6bb52', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_3 = models.CharField(db_column='\u5b57\u6bb53', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_4 = models.CharField(db_column='\u5b57\u6bb54', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_5 = models.CharField(db_column='\u5b57\u6bb55', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_6 = models.CharField(db_column='\u5b57\u6bb56', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_7 = models.CharField(db_column='\u5b57\u6bb57', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_8 = models.CharField(db_column='\u5b57\u6bb58', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_9 = models.CharField(db_column='\u5b57\u6bb59', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_10 = models.CharField(db_column='\u5b57\u6bb510', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. class Meta: managed = False db_table = 'shici' def __unicode__(self): return self.field_1 class Shicibuchong(models.Model): id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase. field_1 = models.CharField(db_column='\u5b57\u6bb51', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_field = models.FloatField(db_column='\u6b21\u6570', blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. Field renamed because it ended with '_'. field_3 = models.CharField(db_column='\u5b57\u6bb53', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_4 = models.CharField(db_column='\u5b57\u6bb54', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_5 = models.CharField(db_column='\u5b57\u6bb55', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_6 = models.CharField(db_column='\u5b57\u6bb56', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_7 = models.CharField(db_column='\u5b57\u6bb57', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_8 = models.CharField(db_column='\u5b57\u6bb58', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_9 = models.CharField(db_column='\u5b57\u6bb59', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_10 = models.CharField(db_column='\u5b57\u6bb510', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_11 = models.CharField(db_column='\u5b57\u6bb511', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. class Meta: managed = False db_table = 'shicibuchong' def __unicode__(self): return self.field_1 class Xuci(models.Model): id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase. field_field = models.CharField(db_column='\u865a\u8bcd', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. Field renamed because it ended with '_'. field_1 = models.CharField(db_column='\u91ca\u4e491', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_2 = models.CharField(db_column='\u91ca\u4e492', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_3 = models.CharField(db_column='\u91ca\u4e493', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_4 = models.CharField(db_column='\u91ca\u4e494', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_5 = models.CharField(db_column='\u91ca\u4e495', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_6 = models.CharField(db_column='\u91ca\u4e496', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_7 = models.CharField(db_column='\u91ca\u4e497', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_8 = models.CharField(db_column='\u91ca\u4e498', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_9 = models.CharField(db_column='\u91ca\u4e499', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_10 = models.CharField(db_column='\u91ca\u4e4910', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. field_11 = models.CharField(db_column='\u91ca\u4e4911', max_length=255, blank=True, null=True) # Field renamed to remove unsuitable characters. Field renamed because it started with '_'. class Meta: managed = False db_table = 'xuci' def __unicode__(self): return self.field_1 (我在这里写了搜索功能)

views.py

search.html

from django.shortcuts import render
from dict.models import Cyshici

def index(request):
    Cyshici_list = Cyshici.objects.all()
    context_dict = {'cyshici': Cyshici_list}
    return render(request, 'dict/base.html', context_dict)

def search(request):

    result_cyshici_list = []

    if request.method == 'GET':
        if 'field_1' in request.GET:
            field_1 = request.GET['field_1']
            try:
                words = Cyshici.objects.filter(field_1=field_1)
                result_cyshici_list.append(words)
            except:
                pass

    return render(request, 'dict/search.html', {'result_cyshici_list': result_cyshici_list})

这是我的base.html。我希望它显示搜索表单。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf8">
</head>
<body>

{{ result_cyshi_list }}

</body>
</html>

HTML正在返回一个空白页面。任何人都可以帮助我理解错误或指向正确的方向吗?

1 个答案:

答案 0 :(得分:1)

您的表单只有一个名为&#c; cyshici&#39;的输入,因此这是您在请求中获得的唯一密钥.GET。 尝试将输入的名称更改为&#39; field_1&#39;,或更改您的观看代码以查找“cyshici&#39;请求中的密钥。改为。