Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';BoxesView';对象没有属性';对象列表';_Python_Django_Django Forms_Django Views - Fatal编程技术网

Python 属性错误:';BoxesView';对象没有属性';对象列表';

Python 属性错误:';BoxesView';对象没有属性';对象列表';,python,django,django-forms,django-views,Python,Django,Django Forms,Django 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_va

试图将表单添加到我的主页视图,并在提交表单时出现此错误。这是我的密码:

视图.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
class UserRegistrationForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User

        fields = [
            'username',
            'password',
        ]
forms.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
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>
{%csrf\u令牌%}
{{form.username}
{{form.password}}

我的回溯指向以下几行:
return self.form\u invalid(form)
&
context=super(BoxesView,self)。get\u context\u data()
。希望这可以告诉您问题所在,但我一直无法解决。有什么想法吗?

如果您使用的是ListView,您应该
使用

class BoxesView(ListView, FormMixin):
    template_name = 'polls.html'
    model = yourModel
或者您应该在get_queryset方法中指定queryset

def get_queryset(self):
    qs = yourModel.objects.all()
    return qs
请注意,
queryset在模板中通过默认名称对象列表进行访问

另外,不要在ListView中使用表单创建对象,而应尝试仅在ListView中列出对象<代码>使用CreateView创建对象
。您的ListView可能如下所示

class BoxesView(ListView):
    template_name = 'polls.html'
    model = yourModel
    paginate_by = 20
有关更多详细信息,请参阅


另外,从您的问题来看,您似乎试图迭代作为上下文数据传递给模板的Post对象。请注意,
重写def get_context_data(self)是为了提供额外的上下文变量,而不是queryset和ListView始终希望在从ListView继承的类中定义model或get_queryset(self)。如果您试图为Post对象创建ListView,请尝试在类中设置
model=Post

我管理一个解决方案,如果类中没有
user\u list
变量,则必须将其放入,但最好重写构造函数,但必须将模型变量放入类中

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

    def __init__(self, *args, **kwargs):
        super(BoxesView, self).__init__(*args, **kwargs)
        self.object_list = self.get_queryset()

它很有用:p

当我已经将queryset作为上下文传递时,为什么需要指定get_queryset()?(
context['posts']=posts
)。另外,当您说使用CreateView创建对象时,您的意思是创建我的表单吗?所以不要在我的ListView中包含表单?我刚才添加了:
qs=yourModel.objects.all()将qs
返回给我的get\u queryset()方法,我得到了相同的错误。我使用
get\u context\u data()的原因是
返回查询集是因为我实际上并不是只返回查询集,而是返回一个由2个查询集组成的列表(Post&Question)。因此,我应该像现在一样在
get\u context\u data()
中构建这个列表,还是应该在
get\u queryset()
中构建它?是的,所以我现在已经这样做了-但是我仍然得到原始的
属性错误:“BoxesView”对象没有属性“object\u list”
。你知道如何解决这个问题吗?试着在你的类中指定model,或者试着重写
get\u queryset(self)
,然后从get\u queryset返回model对象。我意识到当我提交表单时,object\u列表会返回对象。all()expring第一个元素,你知道为什么吗?