2012-07-03 95 views
1

我有一個簡單的筆記,將我的應用程序的一部分合併到一個html頁面中。一頁有表格提交筆記,而另一頁則顯示結果。我希望在同一頁面上顯示錶單和結果。使用表格擴展Django模板

我試圖使用Django的模板繼承來實現這一點,但結果沒有按計劃工作。當我使用它(顯然不正確)時,結果不顯示,只顯示錶單中的「提交」按鈕;不是實際的形式。

這應該是相當簡單的,但我已經嘗試了幾種變化,並研究了幾個小時沒有成功的潛在解決方案和模板文檔。

如果有人能夠說明我做錯了什麼,那將非常感激。

這裏的基本表單模板的代碼:

{% extends 'base.html' %} 
{% block page_title %}Notebook{% endblock %} 
{% block headline %}Notebook{% endblock %} 
{% block content %} 
<h2>Headline</h2> 
    <div class="row"> 
    <div class="span8"> 

<form action="" method="post">{% csrf_token %} 
{{ form.as_p }} 
<input type="submit" class="btn btn-primary" value="Submit" /> 
</form> 
    </div> 

    </div> 

{% endblock %} 

而這裏的attemtped擴展形式將理想包含的形式和結果:

{% extends 'report/write_note.html' %} 


{% block extendscontent %} 
    <div class="span4 offset8"> 
      {% if notes %} 
      {% for note in user.notes.all reversed %} 
      <h3>{{ note.title }}</h3> 
      <p>{{ note.date }}</p> 
      <p>{{ note.copy }}</p> 
      {% endfor %} 

      {% else %} 
      <p>You have no notes stored.</p> 

      {% endif %} 
    </div> 
{% endblock extendscontent %} 

而且這裏的觀點:

@login_required 
def submit_note(request): 
    if request.method == 'POST': 
     notes_form = NotesForm(request.POST, request.FILES) 
     if notes_form.is_valid(): 
      new_note = notes_form.save(commit=False) 
      new_note.author = request.user 
      new_note.save() 
      return HttpResponseRedirect("") 
    else: 
     notes_form = NotesForm() 
    return render_to_response("report/write_note.html", {'form': notes_form}, context_instance=RequestContext(request)) 

@login_required 
def all_notes(request): 
    all_notes = Notes.objects.all().order_by('-date') 
    paginate = paginated_stories(request, all_notes) 
    return render_to_response("report/notes.html", 
          {'notes': paginate}, 
          context_instance=RequestContext(request)) 
+0

你需要顯示視圖。 –

+0

添加了上面的視圖代碼。 –

+0

對不起,你是否試圖在另一個視圖中使用一個視圖中的元素? –

回答

2

我認爲你可以通過結合你的意見,在同一頁面上同時具有表單和筆記。即像 -

@login_required 
def notes_view(request): 
    all_notes = Notes.objects.all().order_by('-date') 
    paginate = paginated_stories(request, all_notes) 
    # form processing and other code 
    # ------------------------------- 
    # return both form and notes to your template context 
    return render_to_response("report/notes.html",{ 
     'notes': paginate, 
     'form': notes_form, 
    },context_instance=RequestContext(request)) 

或者你可以創建一個自定義templatetag無論是渲染的筆記或notes_form

+0

這很好。謝謝你教我一些東西!從來沒有確定如何結合形式和迴應。簡單! –

0

試試這個:

{% extends 'report/write_note.html' %} 

{% block content %} 
    {{ block.super }} 

    <div class="span4 offset8"> 
      {% if notes %} 
      {% for note in user.notes.all reversed %} 
      <h3>{{ note.title }}</h3> 
      <p>{{ note.date }}</p> 
      <p>{{ note.copy }}</p> 
      {% endfor %} 

      {% else %} 
      <p>You have no notes stored.</p> 

      {% endif %} 
    </div> 
{% endblock %} 

當您覆蓋一個塊時,您只需重寫該塊,則不會向其中添加「擴展」。 並且,{{ block.super }}可用於從塊的父版本獲取內容。

+0

這幾乎完美!除了表單域不顯示,只是提交按鈕。否則,筆記也會出現。只需要弄清楚如何讓表單呈現。 –