2017-11-07 1 views
1

현재 제품 ID를 URL에 제공된 ID와 비교하려고합니다. 그러나 테스트에서 동등 함을 제공하더라도 템플릿의 if 문은 항상 "else"를 반환합니다.장고 ifequal 및 if 문 항상 다른 태그로 이동

(를 데이터가 주어집니다)

views.py

{%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 문 작동하지 포함)

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 정상적인 if 태그도 없습니다.

+0

명확하게하기 위해, 평등이 작동하고 if 문이 작동하지 않는 경우? –

+0

@ RyanO'Donnell 어느 쪽도 노력하고 있지 않습니다. – andrewixl

답변

2

product_id은 URL에서 왔기 때문에 정수가 아닌 문자열이됩니다. 그것을 정수로 변환해야합니다.

context = { 
"product":products, 
"theid" : int(product_id), 
} 
파이썬에서

, 그리고 장고 템플릿 언어, '1'1 동일하지 않습니다.