2017-11-07 8 views
1

現在、製品IDとURLで指定されているIDを比較しようとしています。しかし、テストではどちらも等しいとはいえ、テンプレートのif文は常に "else"を返します。 (if文に動作していないと)(データが指定されている)Django ifequalとif文常にElseタグに行く

views.py

def editstatus(request, product_id): 
    try: 
     request.session['user_id'] 
    except KeyError: 
     return redirect("/login") 
    products = Product.objects.all() 
    context = { 
    "product":products, 
    "theid" : product_id, 
    } 
    return render(request, 'posystem/status.html', context) 

status.html

{%for product in product%} 
       <tbody> 
       <tr> 
        <td>{{product.id}}</td> 
        <td>{{theid}}</td> 
        <td>{{product.product_description}}</td> 
        <td>{{product.product_number}}</td> 
        <td>{{product.product_quantity}}</td> 
        <td>{{product.unit_cost}}</td> 
        <td>{{product.final_cost}}</td> 
        <td>{{product.status}}</td> 
        {% ifequal product.id theid %} 
        <h1>hello</h1> 
        {% else %} 
        <h1>hello2</h1> 
        {% endifequal %} 

        {% if theid %} 
        {% if product.id == theid %} 
        <td><select> 
         <option value="5 Votes Needed">5 Votes Needed</option> 
         <option value="Ready to Order">Ready to Order</option> 
         <option value="Needs to Be Signed">Needs to Be Signed</option> 
         <option value="Ordered">Ordered</option> 
         <option value="Recieved">Recieved</option> 
        </select></td> 
        <td><form class="" action="/changestatus/{{product.id}}" method="post"> 
         {% csrf_token %} 
         <button type="submit" name="edit">Save</button> 
        </form></td> 
        {% endif %} 
        {% else %} 
        <td><form class="" action="/status/{{product.id}}" method="post"> 
        {% csrf_token %} 
        <button type="submit" name="edit">Edit</button> 
        </form></td> 
        {% endif %} 

       </tr> 
       </tbody> 
       {% endfor %} 

私はそれがifequalタグでは動作しませんどちらも理由に混乱しています通常のifタグもありません。

+0

明確にするために、等価関数が動作していてif文が正しくない場合は? –

+0

@ RyanO'Donnellどちらも動作していません – andrewixl

答えて

2

product_idはURLからのものなので、整数ではなく文字列になります。整数に変換する必要があります。

context = { 
"product":products, 
"theid" : int(product_id), 
} 
Pythonで

、およびDjangoテンプレート言語、'1'1に等しくありません。