我正在做一个Django项目,我添加了一个用户可以发表评论的功能。但是,我的测试网站上看不到该功能。这是函数add_comment以及我在views.py中导入的内容:
from django.shortcuts import render_to_response
from article.models import Article, Comment
from django.http import HttpResponse
from forms import ArticleForm, CommentForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.utils import timezone
#import pdb; pdb.set_trace()
# Create your views here.
def add_comment(request, article_id):
a = Article.objects.get(id=article_id)
if request.method == "POST":
f = CommentForm(request.POST)
if f.is_valid():
c = f.save(commit=False)
c.pub_date = timezone.now()
c.article = a
c.save()
return HttpResponseRedirect('/articles/get/%s' % article_id)
else:
f = CommentForm()
args = {}
args.update(csrf(request))
args['article'] = a
args['form'] = f
return render_to_response('add_comment.html', args)
在settings.py的INSTALLED_APPS部分,我有:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.comments',
'article',
)
我的add_comment.html文件:
{% extends "base.html" %}
{% block sidebar %}
<ul>
<li><a href="/articles/get/{{article.id}}">Cancel</a></li>
</ul>
{% endblock %}
{% block content %}
<form action="/articles/add_comment/{{article.id}}/" method="post">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" name="submit" value="Post Comment">
</form>
{% endblock %}
我没有收到任何错误消息。评论部分缺失,我不知道为什么。我跑了
python manage.py syncdb
在Ubuntu终端也。我很感激能得到的任何帮助。