2016-12-13 26 views
0

嘗試將表單添加到我的主頁視圖並在提交表單時出現此錯誤。這裏是我的代碼:AttributeError:'BoxesView'對象沒有任何屬性'object_list'

views.py

class BoxesView(ListView, FormMixin): 
    template_name = 'polls.html' 
    form_class = UserRegistrationForm 

    def post(self, request, *args, **kwargs): 
     form = self.get_form() 
     if form.is_valid(): 
      username = form.cleaned_data['username'] 
      password = form.cleaned_data['password'] 
      user = User.objects.create_user(username=username, password=password) 
      user.save() 
      return redirect('/') 
     return self.form_invalid(form) 

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

     context['form'] = self.get_form() 
     question_list = Question.objects.all().order_by('-date') 
     choice = Choice.objects.all() 
     context['question_list'] = question_list 
     context['choice'] = choice 

     q_list = [] 
     returned_list = [] 

     for i in question_list: 
      q_list.append(i) 

     for a, b in CATEGORY_CHOICES: 
      name = resolve(self.request.path_info).url_name 
      if b == name: 
       category = a 

     search = self.request.GET.get('search') 
     posts = Post.objects.all().filter(category=category).order_by('-date') 
     if search: 
      posts = posts.filter(
       Q(title__icontains=search) | 
       Q(content__icontains=search) 
      )   
     else: 
      posts = Post.objects.all().filter(category=category).order_by('-date') 

     context['posts'] = posts 

     total = 0 
     for post in posts: 
      returned_list.append(post) 
      total += 1 
      if total == 4: 
       total = 0 
       for i in q_list: 
        returned_list.append(i) 
        q_list.remove(i) 
        break 

     search = self.request.GET.get('search') 
     posts = Post.objects.all().filter(category=category).order_by('-date') 
     if search: 
      posts = posts.filter(
       Q(title__icontains=search) | 
       Q(content__icontains=search) 
      ) 
     else: 
      posts = Post.objects.all().filter(category=category).order_by('-date') 

     context['posts'] = posts 

     return context 

    def get_queryset(self): 
     pass 

forms.py

class UserRegistrationForm(forms.ModelForm): 
    password = forms.CharField(widget=forms.PasswordInput) 

    class Meta: 
     model = User 

     fields = [ 
      'username', 
      'password', 
     ] 

base.html文件

<form action="" enctype="multipart/form-data" method="post">{% csrf_token %} 
    <div class="registerBox"> 
     {{ form.username }} 
     {{ form.password }} 
     <input type="submit" value="register"/> 
    </div> 
</form> 

我回溯正在將我指向這些行:return self.form_invalid(form) & context = super(BoxesView, self).get_context_data()。希望這可以給你一個什麼問題的跡象,但我一直無法解決它。任何想法?

回答

2

如果您正在使用ListView控件,要麼你應該specify a model attribute

class BoxesView(ListView, FormMixin): 
    template_name = 'polls.html' 
    model = yourModel 

還是應該指定get_queryset方法查詢集。

def get_queryset(self): 
    qs = yourModel.objects.all() 
    return qs 

請注意,queryset is accessed in templates by the default name object_list

而不是在ListView中使用窗體創建對象,只能嘗試列出ListView中的對象。 Use CreateView to create objects。您的ListView可能如下所示。

class BoxesView(ListView): 
    template_name = 'polls.html' 
    model = yourModel 
    paginate_by = 20 

參考this link更多細節

而且從你的問題好像你正試圖遍歷被傳遞到模板上下文數據發佈對象。請注意,Overriding def get_context_data(self) is to provide extra context variables,而不是queryset和ListView始終希望模型或get_queryset(self)在您的類中繼承自ListView中定義。如果你想要做的是爲Post對象創建一個ListView,請嘗試在你的類中設置model=Post

+0

爲什麼我需要指定get_queryset(),當我已經通過查詢集作爲上下文? ('context ['posts'] = posts')。當你說使用CreateView創建對象時,你的意思是創建我的表單?所以不要在我的ListView中包含表單? – Zorgan

+0

此外,我只是添加了:'qs = yourModel.objects.all() 返回qs'到我的get_queryset()方法,我得到相同的錯誤。 – Zorgan

+0

我已經編輯了上面的答案。 hth :) – cutteeth