문제 : - topicstype 필드가 manytomanyfield입니다. 이제 템플릿에서 나는 {{form.topics}}
같은 fieldset 안에있는 div에서 호출하고 있습니다. {{form.topics}}
이 비어 있는지 또는 그 길이가 < = 1인지 확인하고 싶습니다.이 경우 fieldset을 표시하고 싶지 않습니다. {{form.topics}}
여기에 내 코드가 있습니다. jquery를 사용하여이 문제를 해결하고 있습니다.템플릿에서 양식 필드가 비어 있는지 확인하십시오.
forms.py
# Showing only that field to keep code short
class VisitSetupForm(Form):
topics = ModelMultipleChoiceField(
queryset=Topic.objects.filter(reporting=False),
widget=CheckboxSelectMultiple,
required=False
)
Views.py
def setup(request):
if request.user.is_superuser:
form_class = AdminVisitSetupForm
all_topics = True
else:
form_class = VisitSetupForm
all_topics = False
f = form_class(request, data=request.POST or None)
if request.method == "POST":
if f.is_valid():
......so on ....
if request.user.is_superuser:
topics = cd['topics']
else:
topics = set(list(interview.topics.all()) + list(cd['topics']))
next_url = "/visit/confirmation/%s/%s/?next=%s" % (patient.user.id, interview.id, url)
return HttpResponseRedirect(next_url)
if not all_topics:
user = get_user(request)
# checking here if the topics exists for other user
f.fields['topics'].queryset = user.organization.topics
f.fields['interview'].queryset = user.organization.interviews
data['form'] = f
return render_to_response('visit/setup.html', data, context_instance=RequestContext(request))
.html
# calling in html
<fieldset class="step4">
<legend>Step 4 - Topic selection</legend>
<p>Check off any additional topics you want to add to the interview. If you want to
remove a topic from an interview, uncheck it.</p>
<div>{{ form.topics }}</div>
</fieldset>
<script>
if($(".step4 input:checkbox").length <= 0)
{
$(".step4").hide();
}
</script>
{{form.topics}}에는 체크 박스가없는 경우 checkboxes.I의 목록이 원하는되는이 jquery.I을 통해 달성 된 필드 셋 를 표시하지 않습니다 {{form.topics.empty}} 같은 것을 원한다면 step4 필드 세트 을 표시하지 마십시오. 그 jquery를 제거 할 수있는 좋은 방법이 있습니까? 사전에
감사합니다 ..
나는 변수forms.topics의 길이보기에
을 계산하고 단순히 템플릿에서이 변수를 사용하도록 제안
텍스트 단락을 들여 쓰지 마십시오. – Marcin
{% if form.topics %} 구문을 찾으십니까? – akonsu
글쎄, 그냥 form.topics 또는 len (f.fields [ 'topics']. queryset) 길이를 계산 찾고 있는데요. –