Django回复表包含在问题模板中

时间:2010-07-06 11:55:51

标签: django forms templates reply

如果有一个问答系统,答案表格包含在问题模板中(就像facebook后评论一样)还有另一种方法可以保存每个问题的评论吗?我该如何理解问题的内容?

我的代码:

{%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?

THX

3 个答案:

答案 0 :(得分:0)

我建议你在http://docs.djangoproject.com/en/1.2/ref/contrib/comments/#ref-contrib-comments-index阅读有关评论的内容,特别是阅读django / contrib / comments中的代码'render_comment_list'和'render_comment_form',也许你可以使用评论框架就像答案,制作一个“hack “阅读本部分:http://docs.djangoproject.com/en/1.2/ref/contrib/comments/custom/

答案 1 :(得分:0)

另一种方式是在conf或你的urls.py中:

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

并以您的形式:

<form action="/reply/{{ question.id }}/" method="post">

答案 2 :(得分:0)

你的replies.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魔法,你可以让你的回复课回复任何类型的对象,而不仅仅是问题!