我一直在编码,所以我想创建一个像论坛,这是评论部分,如果你们可以帮助我,我会非常感激。
views.py
class createComment(View):
form_class = CommentForm
template_name = "createComment.html"
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form':form})
def post(request):
try:
if request.method == "POST":
form = createComment()
if form.is_valid():
obj = form.save(commit=False)
obj.user_comment = request.user
obj.body_comment = request.POST["body_comment"]
obj.post_comment = request.POST[id]
obj.status_comment = False
obj.save()
return redirect('/')
else:
form = createComment()
except Exception, e:
return render_to_response("errorTemplate.html", {"message" : e.message})
return redirect('/')
models.py
class Comment(models.Model):
user_comment = models.ForeignKey("auth.User")
body_comment = models.TextField()
timestamp_comment = models.DateTimeField(auto_now=True)
post_comment = models.ForeignKey("Post", null=True)
status_comment = models.BooleanField()
def __unicode__(self):
return unicode(self.user_comment)
def __str__(self):
return self.title_comment
答案 0 :(得分:2)
def get(self, request):
...
def post(request):
...
您忘记了self
参数。