'注释'对象没有属性' is_valid'

时间:2018-03-06 19:35:49

标签: python django

我正在制作评论应用以对帖子发表评论。

这是我的comments/forms.py

from django import forms
from .models import Comments
class CommentForm(forms.ModelForm):
    theuser     ='hhh123'
    thepost     ='Bitcoin'
    thecontent  =forms.CharField(widget=forms.TextArea)
    class Meta:
        model=Comments
        fields = ('user','post','content')

这是我的comments/models.py

from django.db import models
from django.conf import settings
from currency.models import Currencies
class Comments(models.Model):
    user        =models.ForeignKey(settings.AUTH_USER_MODEL,default=1)
    post        =models.ForeignKey(Currencies)
    content     =models.TextField()
    timestamp   =models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.user.username

这是我的comments/views.py

from django.conf import settings
from currency.models import Currencies
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render, get_object_or_404, redirect
from .models import Comments
def mycommentview(request):
    instance = Comments.objects.all()
    myformofcomments=Comments(request.POST or None)
    if myformofcomments.is_valid():
        print(myformofcomments.cleaned_data)

    context = {

         "instance" : instance,
         "myformofcomments": myformofcomments,
    }
    return render(request, "home2.html",context)

当我加载页面时,我收到以下错误:

  

/ mycommentview /

中的AttributeError      

评论'对象没有属性' is_valid'

我的错误是什么?

我的环境:

Request Method: GET
Request URL: http://localhost:8000/mycommentview/

Django Version: 1.8
Python Version: 3.5.4
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'mainpage',
 'signup',
 'login',
 'currency',
 'comments',
 'rest_framework',
 'corsheaders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')

错误追溯:

File "C:\Users\vaibhav2\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\vaibhav2\PycharmProjects\MajorProject\src\comments\views.py" in mycommentview
  17.     if myformofcomments.is_valid():

Exception Type: AttributeError at /mycommentview/
Exception Value: 'Comments' object has no attribute 'is_valid'

编辑 - >表格不显示 为方便起见,我将我的模板包含在这里

我的模板==

<form method="POST" action=".">
    {% csrf_token %}
    {{ myform }}

    <input type="submit" value="Submit it">

</form>

1 个答案:

答案 0 :(得分:2)

当您打算使用Comments(这是一种表单)时,您似乎使用了CommentForm(这是一个模型)。