2013-10-13 97 views
2

我正在設計一個頁面,人們可以在其中查看和創建某種排序對象(對象是模型項目的實例)。我不明白如何使用一個模板來顯示兩個視圖(ProjectCreateView和ProjectListView),因爲我理解它,我不能在一個視圖中沒有可怕的混亂代碼,所以我想了解如何使用一個模板來顯示兩個視圖。ListView和CreateView在一個模板Django

眼下,這是我與合作:

views.py:

class ProjectCreateView(CreateView): 
    model = Project 
    template_name = "fileupload/project_list.html" 
    fields = ["name"] 

    def get_context_data(self, **kwargs): 
     context = super(ProjectCreateView, self).get_context_data(**kwargs) 
     return context 

class ProjectListView(ListView): 
    model = Project 

    def get_context_data(self, **kwargs): 
     context = super(ProjectListView, self).get_context_data(**kwargs) 
     return context 

class ProjectView(View): 
    model = Project 
    def get(self, request, *args, **kwargs): 
     view = ProjectListView.as_view() 
     return view(request, *args, **kwargs) 

    def post(self, request, *args, **kwargs): 
     view = ProjectCreateView.as_view() 
     return view(request, *args, **kwargs) 

urls.py

urlpatterns = patterns('', 
    url(r'^projects/$', ProjectView.as_view(), name="projects"), 
) 

models.py

class Project(models.Model): 
    name = models.CharField(max_length=200) 

    def get_absolute_url(self): 
     return reverse("projects") 

代碼形式

<form id="fileupload" method="post" action="." enctype="multipart/form-data"> 
    <div class="row fileupload-buttonbar"> 
     <div class="span7"> 
      <span class="btn btn-primary fileinput-button"> 
       <i class="icon-plus icon-white"></i> 
       <span>New Project</span> 
       <input type="submit" name="Create"> 
      </span> 
      <button type="button" class="btn btn-danger delete"> 
       <i class="icon-trash icon-white"></i> 
       <span>Delete Project</span> 
      </button> 
      <input type="checkbox" class="toggle"> 
     </div> 
     {{ form.as_p }} 
    </div> 
    <table class="table table-striped"><tbody class="files"></tbody></table> 
</form> 

然而,在該結構形式只顯示按鈕後,「名稱」字段已被按下,並輸入一個名字後,我得到這個:

NoReverseMatch at /upload/projects/ 
Reverse for 'projects' with arguments '()' and keyword arguments '{}' not found. 

因此,我猜測,實現這個的方法比我所做的要簡單得多。我會很感激任何幫助。

+0

您可以在一個視圖中實現顯示對象列表,模型表單和處理帖子以創建新對象,而不會讓它變得醜陋,臭或其他。基於類的視圖非常棒,直到他們沒有。 – Brandon

+0

我該怎麼做?我誠實地尋找最乾淨的解決方案,讓我在一個模板上做兩件事。 – Plasma

+0

爲你舉個例子。 – Brandon

回答

7

一個未凌亂功能爲主視圖列出和創建對象...

from django.shortcuts import render 
# model and form imports 

def list_and_create(request): 
    form = YourModelForm(request.POST or None) 
    if request.method == 'POST' and form.is_valid(): 
     form.save() 

    # notice this comes after saving the form to pick up new objects 
    objects = YourModel.objects.all() 
    return render(request, 'your-template.html', {'objects': objects, 'form': form}) 
+0

謝謝!這工作得很好。 – Plasma

+2

不客氣。基於分類的觀點理論上很好,但有時他們只是讓事情變得過於複雜。 – Brandon

8

隨着CreateView的它容易使一個沒有任何複雜可言,不要相信CBV仇敵;)

class ListAndCreate(CreateView): 
    model = YourModel 
    template_name = "your-template.html" 

    def get_context_data(self. **kwargs): 
     context = super(ListAndCreate, self).get_context_data(**kwargs) 
     context["objects"] = self.model.objects.all() 
     return context 

沒有循環,沒有條件意味着沒有測試它的問題。

+0

這很簡單 - 我必須嘗試! – stephendwolff