1

如果有一个问答系统,其中答案表格包含在问题模板中,(就像 facebook 帖子评论一样)是否有另一种方法可以保存每个问题的评论?我怎样才能得到问题的ID?

我的代码:

{%include "replies/replies.html"%} #thats in the template where questions are listed

save_question 视图

def save_reply(request, id):
   question = New.objects.get(pk = id)
   if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
           new_obj = form.save(commit=False)
           new_obj.creator = request.user 
           u = New.objects.get(pk=id)
           new_obj.reply_to = u   
           new_obj.save()
           return HttpResponseRedirect('/accounts/private_profile/')    
   else:
           form = ReplyForm()     
   return render_to_response('replies/replies.html', {
           'form': form,
           'question':question, 
           }, 
          context_instance=RequestContext(request))  

和形式:

<form action="." method="post">
<label for="reply"> Comment </label>
<input type="text" name="post" value="">
<p><input type="submit" value="Comment" /></p>
</form>

我怎样才能让这个表格“嵌入”到问题模板中,我怎样才能让它“知道”它所指的问题的ID?

谢谢

4

3 回答 3

0

我建议您阅读http://docs.djangoproject.com/en/1.2/ref/contrib/comments/#ref-contrib-comments-index上的评论,特别阅读 django/contrib/comments 标签中的代码 'render_comment_list ' 和 'render_comment_form',也许您可​​以使用评论框架之类的答案,使“黑客”阅读此部分:http ://docs.djangoproject.com/en/1.2/ref/contrib/comments/custom/ 。

于 2010-07-06T12:51:35.187 回答
0

另一种方法是在 conf 或您的 urls.py 中:

(r'^reply/(?P<id>\d+)/$',save_reply),

并以您的形式:

<form action="/reply/{{ question.id }}/" method="post">
于 2010-07-06T13:00:57.140 回答
0

在您的回复.html 中有:

<form action="." method="post">
    <input type="hidden" value="{{ in_reply_to_id }}" />
    <label for="reply"> Comment </label>
    <input type="text" name="post" value="">
    <input type="submit" value="Comment" />
</form>

然后在您的问题模板中:

<div class="question" id="question-{{ question.id }}">
    {{ question.text }}
    {% with question.id as in_reply_to_id %}
        {%include "replies/replies.html" %}  <--- in_reply_to_id is sent to the include
    {% endwith %}
</div>

这样,您的主模板可以调用

<p> questions here! <p>
<div class="question-list">
{% for question in question_list %}
    {% include "questions\question.html" %}
{% endfor %}
</div>

包括一点 ContentTypes 魔法,你可以让你的回复类回复任何类型的对象,而不仅仅是问题!

于 2010-07-08T08:47:58.700 回答