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}
,那么犯错误的机会就少了。比我的朋友多了