2017-12-27 5 views
0

장고 템플릿 내 선택 상자에서 값을 선택하지 않았습니다. 다음 코드가 있습니다.django 템플릿 <select> 상자가 값을 선택하지 않습니다

template_name.html

<select name="class_id" > 
{% for object in classes %} 
    <option value="{{ object.id }}" 
     {% if object.id == class_id %} selected="selected" {% endif %}> 
     {{ object.name }} 
    </option> 
{% endfor %} 
</select> 

view.py

class_id = request.POST.get('class_id', 0) 
context_data = {'class_id':class_id} 
return render(request, "template_name.html",context_data) 

우리가 내가 다음과 같은 출력을 얻을

{% for object in classes %} 
    {{ object.id }} = {{ class_id }} <br> 
{% endfor %} 

다음 템플릿에이 코드를 실행하기 때문에이 문제가 이상하다

1 = 3

2 = 3

3 = 3

답변

2

그게 당신이 게시물에서 점점 사람은 기본적으로 문자열이지만보기에 INT

object.id 때문에

class_id = request.POST.get('class_id', 0) 
context_data = {'class_id':int(class_id)} 
+0

고맙습니다. –

관련 문제