Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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 ';评论';对象没有属性';是否有效';_Python_Django - Fatal编程技术网

Python ';评论';对象没有属性';是否有效';

Python ';评论';对象没有属性';是否有效';,python,django,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) cla

我正在制作一个评论应用程序,对帖子发表评论

这是我的
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')
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
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)
这是我的
comments/models.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')
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
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)
这是我的
comments/views.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')
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
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/

注释“对象没有属性”无效

我的错误是什么??

我的环境:

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>

{%csrf_令牌%}
{{myform}}

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

myformofcomments=Comments(request.POST或None)
应该是
myformofcomments=CommentForm(request.POST或None)
?您尚未显示您的模板,因此我们无法说明为什么它没有显示任何内容。您的变量
myformofcomments
有点不寻常。如果你在视图中使用
form
,在模板中使用
{{form}
,那么犯错误的机会就少了。比我的朋友多了