質問

I have a note like system see image here: http://www.uploadscreenshot.com/image/1091218/5818195 and when clicked on note you can see the title, the message and comments. I send them trough js file and set them in the view. My question is now can I do that with the django-comments form? If I just paste in into the template and inside the div that is shown on the bootstrap popup with {% for note in notes %} it shows all the forms for every not on that window (it's understood).

How do I pass the right values to the django-comments form?

This is the js function (just relevant part):

 request.done(function(note) {
    $('h3#view-note-title').text(note.title);
    $('p#view-note-desc').text(note.message);
    var html = '';
    for(var i=0; i<note.comments.length; i++) {
        var item = note.comments[i];
        html += "<p id='comments' style='display: block; background: #a3d95d;margin-bottom: 3px;'>" + item.comment + "</p>";
        html += "<p id='username' style='display: block;background: #edac65;margin-bottom: 3px;'>" + item.username + "</p>";
        html += "<p id='date' style='display: block;background: #afe9eb;margin-bottom: 13px;'>"+ item.submit_date +"</p>";
    }
    $('div#comments').html(html);
});

and this is the relevant part of views.py:

 if request.method == "GET" and request.is_ajax:
    note = get_object_or_404(Note, pk=request.GET['noteid'])
    ctype = ContentType.objects.get_for_model(Note)
    latest_comments = Comment.objects.filter(is_public=True, is_removed=False, content_type=ctype, object_pk=note.id).order_by('-submit_date')[:5]
    response_data = {}
    response_data['title'] = note.title
    response_data['message'] = note.message
    response_data['comments'] = [
        {'username': c.user.username, 'comment': c.comment, 'submit_date': c.submit_date} for c in latest_comments]
    return HttpResponse(json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json")

I hope I was clear enough.

役に立ちましたか?

解決

The solution was to send the CommentForm trough the view.

The code: template

<form id="form_comments" action="{% comment_form_target %}" method="post">
{% csrf_token %}
<table>
<tr>
  <td colspan="2">
    <div class="kopce"></div>
    <input type="submit" name="submit" value="Post">
    <input type="submit" name="preview" value="Preview">
  </td>
</tr>

The view:

from django.contrib.comments.forms import CommentForm

form = CommentForm(target_object = note)
response_data["form_html"] = form.as_p()

The js:

 $('form#form_comments div.kopce').html(note.form_html);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top