2016-04-15 4 views
7

इसलिए मेरे पास यह टिप्पणी फ़ॉर्म है जो post.html में ठीक काम करता है और अब मैं इसे टिप्पणी में उपयोग करने की कोशिश कर रहा हूं Thread.html लेकिन मेरे पास है कुछ समस्या है। (यह दिया मुझे तीन दिनों के लिए गुस्सा दिलाना है, किसी भी मदद की सराहना की जाएगी) मैं अपने कोडबस एक टेम्पलेट में किसी अन्य टेम्पलेट में उपयोग किए गए फॉर्म का उपयोग करने का प्रयास कर रहा है

def post(request, slug): 
     hotCat = Category.objects.get_hotCat() 


     post = get_object_or_404(Post, slug=slug) 
     post.views += 1 # increment the number of views 
     post.save()  # and save it 
     profile = post.moderator 
     #path = request.get_full_path() 
     #comments = Comment.objects.filter(path=path) 
     comments_count = Comment.objects.filter(post=post).count() 
     comments = post.commented_post.all() 
     for c in comments: 
       c.get_children() 
     hidden_data = { 
        "post_id" : post.id, 
        "origin_path" : request.get_full_path, 
        "parent_id" : None 
       } 
     comment_form = CommentForm(hidden_data=hidden_data) 
     context_dict = { 
      'post' :post, 
      'hotCat' :hotCat, 
      'comments' : comments, 
      'comment_form':comment_form, 
      'comments_count':comments_count, 
      'profile' :profile, 


     } 
     return render(request, 'main/post.html', context_dict) 

ऊपर post.html के लिए मेरे कार्य है साथ का तरीका बताएंगे। जो मुख्य निर्देशिका में है। अब निर्देशिका टिप्पणी करने के लिए आगे बढ़ने के लिए, मैं टिप्पणी निर्देशिका में मेरी टिप्पणी फार्म पोस्ट करेंगे

class CommentForm(forms.Form): 
    comment = forms.CharField(
     widget=forms.Textarea(attrs={"placeholder": "leave"}) 
    ) 
    #hidden_field = forms.CharField(widget=forms.HiddenInput()) 

    def __init__(self, hidden_data=None, data=None, files=None, **kwargs): 
     super(CommentForm, self).__init__(data, files, kwargs) 
     self.helper = FormHelper() 
     self.helper.form_show_labels = False 
     self.helper.add_input(Submit('submit', 'leave', css_class='btn btn-default')) 
     if hidden_data: 
      self.helper.add_input(Hidden('post_id', hidden_data['post_id'])) 
      self.helper.add_input(Hidden('origin_path', hidden_data['origin_path'])) 
      if hidden_data.get('parent_id', None): 
       self.helper.add_input(Hidden('parent_id', hidden_data['parent_id'])) 

और, इस टिप्पणी बनाने के लिए मेरे views.py है। इसमें दो भाग हैं, एक parent_comment के लिए और एक parent_comment का जवाब देने के लिए जिसे मैंने child_comment नाम दिया है।

अब मेरे पोस्ट.html में, मुझे दोनों फीचर की आवश्यकता थी। लेकिन commentThread.html के लिए, parent_comment दिखाया गया है और मुझे एक ऐसा फॉर्म चाहिए जो उपयोगकर्ताओं को parent_comment का उत्तर देने की अनुमति देता है। तो मुझे टिप्पणी में क्या चाहिए थ्रेड.html सिर्फ child_comment फॉर्म है।

@csrf_exempt 
def comment_create_view(request): 
    if request.method == "POST" and request.user.is_authenticated(): 
     parent_id = request.POST.get('parent_id') 
     post_id = request.POST.get("post_id") 
     origin_path = request.POST.get("origin_path") 
     try: 
      post = Post.objects.get(id=post_id) 
     except: 
      response_dat = {"code":400,"message":"Post does not exists"} 
      return JsonResponse(response_data) 


     parent_comment = None 
     if parent_id is not None: 
      try: 
       parent_comment = Comment.objects.get(id=parent_id) 
      except: 
       parent_comment = None 

      if parent_comment is not None and parent_comment.post is not None: 
       post = parent_comment.post 

     form = CommentForm(data=request.POST) 
     if form.is_valid(): 
      comment_text = form.cleaned_data['comment'] 
      if parent_comment is not None: 
       # parent comments exists 
       new_comment = Comment.objects.create_comment(
        user=MyProfile.objects.get(user=request.user), 
        path=parent_comment.get_origin, 
        text=comment_text, 
        post = post, 
        parent=parent_comment 
        ) 

       hidden_data = { 
        "post_id" : post.id, 
        "origin_path" : request.get_full_path, 
        "parent_id" : parent_comment.id 
       } 
       comment_form = CommentForm(hidden_data=hidden_data) 
       html = render_to_string('main/child_comment.html', {'comment': [new_comment], 
             'user': request.user, 
             'comment_form':comment_form}) 
       response_data = {"status":200, "message":"abc~", 
               "comment":html, 
               'parent': True, 
               'parent_id': parent_comment.id, 
               'comment_count': parent_comment.comment_count()} 
       return JsonResponse(response_data) 
      else: 
       new_comment = Comment.objects.create_comment(
        user=MyProfile.objects.get(user=request.user), 
        path=origin_path, 
        text=comment_text, 
        post = post 
        ) 

       hidden_data = { 
        "post_id" : post.id, 
        "origin_path" : request.get_full_path, 
        "parent_id" : None 
       } 
       comment_form = CommentForm(hidden_data=hidden_data) 
       html = render_to_string('main/parent_comment.html', {'comment': new_comment, 
             'user': request.user, 
             'comment_form':comment_form}) 
       response_data = {"status":200, "message":"abc~", "comment":html, 'parent': False} 
       return JsonResponse(response_data) 

     else: 
      print str(form) 
      messages.error(request, "There was an error with your comment.") 
      response_data = {"status":400,"message":"There was an error with your comment."} 
      return JsonResponse(response_data) 

    else: 
     raise Http404 

post.html रूप में एक ही दृष्टिकोण के साथ, मैं commentThread.html के लिए दृश्य बनाया है और नीचे है मैं क्या किया था, मुझे पता है कि मैं गलत यहाँ किया तो कृपया;

def comment_thread(request, id): 
    hotCat = Category.objects.get_hotCat() 

    comment = Comment.objects.get(id=id) 
    comments = comment.post.commented_post.all() 
    for c in comments: 
      c.get_children() 
    hidden_data = { 
       "post_id" : comment.post.id, 
       "origin_path" : request.get_full_path, 
       "parent_id" : None 
      } 
    comment_form = CommentForm(hidden_data=hidden_data) 
    context = { 
    "comment": comment, 
    'comment_form':comment_form, 
    "hotCat":hotCat 
    } 
    return render(request, "comments/comment_thread.html", context) 

पोस्ट.html में, टिप्पणी प्रदर्शित करने के लिए मेरे पास तीन टेम्पलेट थे। post.html, parent_comment.html, और child_comment.html। मैं उन्हें यहां भी पोस्ट करूंगा।

post.html

{% if user.is_authenticated %} 

<!-- <form method="POST" id="commentForAjax" class='form-comment'>{% csrf_token %} 

<input type='hidden' name='post_id' value='{{ post.id }}'/> 
<input type='hidden' name='origin_path' value='{{ request.get_full_path }}'/> --> 

{% crispy comment_form comment_form.helper %} 
<!-- </form> --> 

{% endif %} 


<div class="comment_bottom" style="padding:3px;"> 
{% if user.is_authenticated %} 
<div class="make_reply"> 
    <a href='#' class='reply_btn'>reply</a> 
     <div class='reply_comment'> 

     <!-- <form method="POST" id="childCommentForAjax" class='form-comment'>{% csrf_token %} 
     <input type='hidden' name='post_id' id='post_id' value='{{ post.id }}'/> 
     <input type='hidden' name='origin_path' id='origin_path' value='{{ request.get_full_path }}'/> 
     <input type='hidden' name='parent_id' id='parent_id' value='{{ comment.id }}' /> --> 

     {% crispy comment_form comment_form.helper %} 
     <!-- </form> --> 
     </div> 
    </div> 
{% endif %} 

<script> 
$(document).ready(function() { 


    $(document).on('submit', 'form', function(e){ 
     e.preventDefault(); 
     if($(this).parents("tr").length != 0) { 
     parent_id = $(this).parents("tr").attr("id").split("_")[1]; 
     data_str = $(this).serialize() + "&parent_id=" + parent_id; 
     } else { 
     data_str = $(this).serialize(); 
     } 
     $.ajax({ 
     type:'POST', 
     url:'/comment/create/', // make sure , you are calling currect url 
     data:data_str, 
     success:function(json){    
      alert(json.message); 
      if(json.status==200){ 
      var comment = json.comment.trim(); 
      var user = json.user; 
      /// set `comment` and `user` using jquery to some element 
       if(!json.parent) { 
       $(comment).insertBefore('.table tr:first'); 
       } 
       else { 
       $(comment).insertBefore('#comment_' + json.parent_id + ' #child_comment:first'); 
       $(".replies").text("reply" + json.comment_count + "view all"); 
       } 
      } 

     }, 
     error:function(response){ 
      alert("some error occured. see console for detail"); 
     } 
     }); 
    }); 


$('#comment-post-form').on('submit', function(event){ 
    event.preventDefault(); 
    console.log("form submitted!"); // sanity check 
    create_post(); 
}); 

</script> 

ajax के लिए नीचे दिए गए मेरे parent_comment.html

{% load crispy_forms_tags %} 

<tr id="comment_{{ comment.id }}"> 
    <td> 
    <div class="row"> 
     <div class="col-sm-1"> 
    <a href="{% url 'userena_profile_detail' comment.user.user %}"><img src="{{ comment.user.get_mugshot_url }}" height='48' width='48' /></a> 
     </div> 
     <div class="col-sm-11"> 
     <div class="row"> 
      <div class="col-sm-12"> 
      <p> <a href="{% url 'userena_profile_detail' comment.user.user %}" style="padding:5px;">{{ comment.user.user }}</a>| <small>{{ comment.timestamp|timesince }} </small></p> 

      </div> 
     </div> 
     <div class="row"> 
    <span style="margin:5px; word-break: break-all;"> 
    {{ comment.get_comment }} 
</span> 

     </div> 
     </div> 
    </div> 

    <div class="comment_bottom" style="padding:3px;"> 
{% if user.is_authenticated %} 
<div class="make_reply"> 
    <a href='#' class='reply_btn'>reply</a> 
     <div class='reply_comment'> 

     <form id="childCommentForAjax" method="POST">{% csrf_token %} 
     <input type='hidden' name='post_id' id='post_id' value='{{ post.id }}'/> 
     <input type='hidden' name='origin_path' id='origin_path' value='{{ comment.get_origin }}'/> 
     <input type='hidden' name='parent_id' id='parent_id' value='{{ comment.id }}' /> 
     {% crispy comment_form comment_form.helper %} 

     </form> 
     </div> 
    </div> 
{% endif %} 


    <div class="replyInfo"> 
    {% if not comment.is_child %} 
     <div class="wholeReply"> 
      {% if comment.comment_count %} 
     <a href='#' class='replies'>   
     {{comment.comment_count}}</a> 
      {% endif %} 

    <div class="got_replies"> 
     <ul id="child_comment" style="list-style-type: none;"> 
     {% for child in comment.get_children %} 
<hr>   
     <li> 
      <div class="row"> 
      <div class="col-sm-1"> 
<a href="{% url 'userena_profile_detail' child.user.user %}" style="float:left;"><img src="{{ child.user.get_mugshot_url }}" height='48' width='48'/></a>    
      </div> 
      <div class="col-sm-11"> 
       <div class="row"> 
       <div class="col-sm-12"> 
        <p><a href="{% url 'userena_profile_detail' child.user.user %}" style="float:left;">&nbsp; {{ child.user.user }}</a>| <small>{{ comment.timestamp|timesince }} </small></p> 
       </div> 
       </div> 
       <div class="row"> 
       <div class="col-sm-12"> 
        <span style="word-break: break-all; margin:5px;"> 
    {{ child.get_comment }}</span> 
       </div> 
       </div> 
      </div> 
      </div> 
     </li> 
     {% endfor %} 
     </ul> 
    </div> 





    </div> 
    </div> 
</div> 
</div> 
</div> 
     {% endif %} 


</td></tr> 

और नीचे मेरी child.html

{% load crispy_forms_tags %} 

<ul id="child_comment" style="list-style-type: none;"> 
     {% for child in comment %} 
<hr>   
     <li> 
      <div class="row"> 
      <div class="col-sm-1"> 
<a href="{% url 'userena_profile_detail' child.user.user %}" style="float:left;"><img src="{{ child.user.get_mugshot_url }}" height='48' width='48'/></a>    
      </div> 
      <div class="col-sm-11"> 
       <div class="row"> 
       <div class="col-sm-12"> 
        <p><a href="{% url 'userena_profile_detail' child.user.user %}" style="float:left;">&nbsp; {{ child.user.user }}</a>| <small>{{ child.timestamp|timesince }} </small></p> 
       </div> 
       </div> 
       <div class="row"> 
       <div class="col-sm-12"> 
        <span style="word-break: break-all; margin:5px;"> 
    {{ child.get_comment }}</span> 
       </div> 
       </div> 
      </div> 
      </div> 
     </li> 
     {% endfor %} 
     </ul> 

तो commentThread.html में है, मेरे द्वारा किए गए फॉर्म के लिए

<div> 
{% crispy comment_form comment_form.helper %} 

</div> 

मैं मैं वहाँ डाल ही ajax समारोह लेकिन कोई फर्क नहीं डाल करने की कोशिश की ..

==> पेज कुछ भी बदल रहा है ... बिना ताजा कर कोई त्रुटि यह सिर्फ ताज़ा किया जाता है पता चलता हो जाता है ... कोई डेटा को बचा लिया। ...]]

क्या कोई मेरी मदद कर सकता है? मैंने जितना संभव हो सके विस्तृत

+2

हमें शायद जावास्क्रिप्ट की आवश्यकता है। – snakecharmerb

+0

ऐसा लगता है कि सभी जानकारी यहां है। क्या आप क्लाइंट साइड डीबग करने का प्रयास कर सकते हैं? आप किस ब्राउजर का उपयोग कर रहे हैं? –

+0

@ MichałZaborowski मैं क्रोम – winixxee

उत्तर

2

HTML में <form></form> टैग के अंदर फ़ॉर्म शामिल करें क्योंकि यह parent_comment.html में किया गया है। और अपनी टिप्पणी में post.html के <script> ब्लॉक का भी उपयोग करें Thread.html। समस्या में फॉर्म तत्व की अनुपस्थिति के कारण समस्या प्रतीत होती है। थ्रेड.html

संबंधित मुद्दे