2012-02-09 4 views
0

문제 : - topicstype 필드가 manytomanyfield입니다. 이제 템플릿에서 나는 {{form.topics}} 같은 fieldset 안에있는 div에서 호출하고 있습니다. {{form.topics}}이 비어 있는지 또는 그 길이가 < = 1인지 확인하고 싶습니다.이 경우 fieldset을 표시하고 싶지 않습니다. {{form.topics}} 여기에 내 코드가 있습니다. jquery를 사용하여이 문제를 해결하고 있습니다.템플릿에서 양식 필드가 비어 있는지 확인하십시오.

는 ({{form.topics이 비어}})
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의 길이보기에

을 계산하고 단순히 템플릿에서이 변수를 사용하도록 제안

+0

텍스트 단락을 들여 쓰지 마십시오. – Marcin

+0

{% if form.topics %} 구문을 찾으십니까? – akonsu

+0

글쎄, 그냥 form.topics 또는 len (f.fields [ 'topics']. queryset) 길이를 계산 찾고 있는데요. –

답변

1

{% if not forms.topic or variable <= 1 %} 
    <td>Whatever you want to display</td> 
{% else %} 
    <td> {{ forms.topic }} </td> 
{% endif %} 

이 코드는 "forms.topic"또는 변수의 길이 (보기에서 계산 한 값)의 값이 1보다 작거나 같아야합니다. 표시 할 텍스트를 인쇄하십시오.

+0

정확히 문제는 내가 변수 = len (f.fields [ 'topics'])를 사용하고 있습니다. queryset) 또한 카운트를 사용하고 그것을 매니저 len() 속성을 나타냅니다. 해당 필드의 길이를 계산 한 후 나는 템플릿에서 사용할 수 있습니다. 어떻게 길이를 계산합니까? 내가 뭘하고있어? –

+0

@the_game : 파이썬 셸을 열어서 f.fields [ 'topics']가 무엇인지 확인해야한다고 생각합니다. 쿼리 세트가 생성하는 값, 특정 값을 가져올 수 있는지 여부, 튜플,리스트 또는 문자열이 될 수 있습니다. . 먼저 확인하십시오. – Prateek

관련 문제