表单字段未在django html中显示

时间:2014-07-17 16:31:28

标签: python django

我已经编写了一个代码来制作表格,我不知道为什么这里没有显示字段是我的代码。

我想从index.html页面获取请求并使用该请求,我运行了一个查询以在同一页面上显示查询结果。

views.py
def search123(request):
  searc=search1()
  if request.method=="POST":        
        searc=search1(request.POST or None)
        if searc.is_valid():
            if 'd_box' in request.POST:
                item_map=item.objects.raw('SELECT * FROM `item` WHERE `category_id`=%s', [request.POST['d_box']])
                lis=[]
                for e in (item_map):
                    lis.append(e.id)
                price_map=item_done.objects.filter(item_id__in=lis).order_by('item_id')
                return render_to_response('index.html',{'posts':price_map},RequestContext(request))
  return render_to_response('index.html',{'posts':searc},RequestContext(request))


index.html

<html>
<head>   
</head>

<body>
<form method='POST' action=''>{% csrf_token %}
<h4> SEARCH </h4>
{{searc.as_table}}
<input type='submit' name="button7" value="button7">
</form>


{% regroup posts by item_id as post_list %}

<table border="4" style="width:1050px;border-collapse: collapse" >
    <thead>
            <tr>
            <th>Item Codes</th>
            <th>Name</th>
            <th>MRP</th>
            <th>Site price</th>
            <th>Website</th>
            </tr>

          </thead>        
     <tbody>           


{% for country in post_list %}

    <tr>
    <td>{{ country.grouper }}</td>
    <td>
        {% for item in country.list %}
        <div style="margin: 5px 5px">{{item.name}}</div>
        {% endfor %}    
    </td>


       <td>
        {% for item in country.list %}
        <div style="margin: 5px 5px">{{item.mrp}}</div>
        {% endfor %}    
    </td>

    <td>
        {% for item in country.list %}
        <div style="margin: 5px 5px">{{item.site_price}}</div>
        {% endfor %}    
    </td>

    <td>
        {% for item in country.list %}
        <div style="margin: 5px 5px">{{item.crawl_id}}</div>
        {% endfor %}    
    </td>


    </tr>
{% endfor %}

     </tbody>
</table>
</html>


forms.py
class search1(forms.ModelForm):
    class Meta:
        model=search
        exclude=[]

1 个答案:

答案 0 :(得分:0)

我认为您必须在search中导入forms.py模型,并且您在模板中写入错误的表单名称!你写{{searc.as_table}}它必须是{{search.as_table}}