2017-11-16 9 views
0

私は脆弱な検索バーを作成したいが、すべてを行ったが残念なことに、submitを押すと検索機能に結果が表示されない。 しかし、端末は成功を示し、 "POST /注入/ HTTP/1.1" 200 596脆弱なDjango検索バーを作成する

views.py 
    @csrf_exempt 
    def search_form(request): 
     if 'searchField' in request.POST: 
      query= "SELECT * FROM injection_search WHERE injection_search.name LIKE '%searchField%'" 
      print(query) 
      item = search.objects.raw(query) 

     else: 
      item = search.objects.all()  
     return render(request, 'home.html', {'item' : item}) 

template/home.html 

{% load static %} 
<link href="{% static 'css/style.css' %}" rel="stylesheet"></link> 
<h1 class="page-header">INJECTION</h1> 

<form action="" method="POST"> 
<input type="text" name="searchField" id="searchField" placeholder="search trainers.."> 
<button type="submit">Find</button> 
</form> 

<h1> Search Page </h1> 
<h3> Injection demo</h3> 
<div class="shopping-container"> 

    <table border="3" cellspacing="0" cellpadding="10"> 
     <tbody> 
      <tr> 
       <th>Title</th> 
       <th>Description</th> 
       <th>Quantity</th> 
      </tr> 
      <!--{% for search in item %}--> 
      <tr> 
      <td>{{ search.name}}</td> 
      <td>{{ search.description }}</td> 
      <td>{{ search.price}}</td> 
      </tr> 
      <!--{%endfor%}--> 
    </tbody> 
    </table> 
</div> 

model.py 
app_name = 'injection' 
urlpatterns = [ 
    url(r'^$', views.index, name='index'), 
    url(r'^injection/$', views.search_form, name='search_form'), 
] 
+0

あなたが今までSQLに掲示値を渡しません。 –

+0

申し訳ありません、あなたは私のために少し説明できますか?事前に感謝します –

+0

私のsearchFieldは私の検索フィールドを表しているので、私はここに投稿していると思いますが、間違っているかもしれません。 –

答えて

0

あなたのSQL文字列での変数を使用していないので、これは、どちらかのPHP上で動作しないでしょう。あなたは単にリテラル値 "searchField"を使用しています。

コードを動作させるには、実際にはSQLコマンドに文字列の値を入れる必要があります。 Pythonではこれを行うための複数の方法があり、一つの例は次のようになります。

query= "SELECT * FROM injection_search WHERE injection_search.name LIKE '%{}%'".format(searchField) 
+0

ありがとうございます、それは今働いています。 –

関連する問題