【问题标题】:Some wrongs when i want to achieve search function in Django当我想在 Django 中实现搜索功能时的一些错误
【发布时间】:2015-09-23 12:08:46
【问题描述】:

这是我的models.py(模型是由 django 从 MySQL 数据库中提取的):

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(我在这里写了搜索功能)

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

搜索.html

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

{{ result_cyshi_list }}

</body>
</html>

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

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

    <form method="get" action="/dict/search/" enctype="mutipart/form-data">
    <div class="form-group">
         <p><input class="form-control" id="search_cyshici" name="cyshici" /></p>
    </div>
</form>

</body>
</html>

HTML 正在返回一个空白页面。谁能帮助我了解问题所在或指出正确的方向?

【问题讨论】:

  • 你的表格应该被张贴...
  • @aryan: 表格也可以搞定
  • @aryan 为什么需要发帖??? “获取”不代表获取数据?
  • 去掉 try 和 except 可能会报错
  • 您的模型肯定有very_1very_2very_3 ...very_27错误,还是现有表格有very_1 ..very_27错误?

标签: python mysql django search


【解决方案1】:

您的表单只有一个名为“cyshici”的输入,因此这是您在 request.GET 中获得的唯一键。 尝试将输入的名称更改为“field_1”,或更改您的视图代码以在 request.GET 中查找“cyshici”键。

【讨论】:

  • 是的!我已经注意到了这个问题,我已经改变了这一点。但无论如何,我真的很感谢你的帮助!!!!
猜你喜欢
  • 2018-08-25
  • 2019-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
相关资源
最近更新 更多