2017-06-23 14 views
26

के लिए काम नहीं कर रहा है मेरे पास 2 प्रश्नपत्र हैं: पोस्ट और टिप्पणी। मैं AJAX का उपयोग करके इन्हें प्रस्तुत करने के लिए django-el-pagination का उपयोग कर रहा हूं।एकाधिक पेजिनेशन (AJAX) django-el-pagination

यहाँ मेरे विचार है:

def profile(request, user, extra_context=None): 

    profile = Profile.objects.get(user__username=user) 

    page_template = 'profile.html' 

    if request.is_ajax(): 
     user_queryset = request.GET.get('user_queryset') 
     print('Queryset:', user_queryset) 
     if user_queryset == 'user_posts': 
      page_template = 'user_posts.html' 
     elif user_queryset == 'user_comments': 
      page_template = 'user_comments.html' 
     else: 
      pass 

    print('Template:', page_template) 

    user_posts = Post.objects.filter(user=profile.user).order_by('-date') 
    user_comments = Comment.objects.filter(user=profile.user).order_by('-timestamp') 

    context = {'user_posts': user_posts,'user_comments': user_comments, 'page_template': page_template} 

    if extra_context is not None: 
     context.update(extra_context) 

    return render(request, page_template, context) 

मैं एक ajax कॉल कि पता लगाने के जो क्वेरी सेट का इस्तेमाल किया जा रहा है। तो जब अधिक पेजिनेटेड ऑब्जेक्ट्स प्राप्त करने के लिए 'अधिक टिप्पणियां' या 'अधिक पोस्ट' (टेम्पलेट में) क्लिक किया जा रहा है, तो मुझे पता है कि यह कौन सी क्वेरीसेट है। हालांकि जब मैं उपरोक्त कोड का उपयोग करता हूं और AJAX पेजिनेशन के लिए 'अधिक' पर क्लिक करता हूं, तो यह पूरे पृष्ठ को जोड़ता है, प्रासंगिक बच्चे टेम्पलेट (user_posts.html या user_comments.html) नहीं। लेकिन if request.is_ajax() कोड ब्लॉक ठीक काम करता है; यह उपयोग करने के लिए सही टेम्पलेट प्रिंट करता है यह हो रहा नहीं होना चाहिए।

जब मैं Post कार्यों के लिए इस

if request.is_ajax(): 
    page_template = 'user_posts.html' 

ajax पृष्ठांकन है कि कोड ब्लॉक बदल जाते हैं। हालांकि मैं अच्छी तरह से। क्यों नहीं करता Comment के लिए ajax पृष्ठांकन जोड़ना चाहते हैं मेरा प्रारंभिक if request.is_ajax() काम और मैं इसे कैसे ठीक कर सकता हूं?

संपादित करें:

जब मैं more posts पर पर क्लिक करें के आउटपुट:

Queryset: None 
Template: profile.html 
Queryset: user_posts 
Template: user_posts.html 

js

$('body').on('click', '.endless_more', function() { 
    console.log($(this).html()); #works successfully 
    var user_queryset; 
    if ($(this).html() === 'more posts') { 
     console.log('POSTS'); #works successfully 
     var user_queryset = 'user_posts' 
    } else if ($(this).html() === 'more user comments') { 
     user_queryset = 'user_comments'; 
     console.log('COMMENTS'); #works successfully 
    } else { 
     console.log('none'); 
    } 
    $.ajax({ 
     type: 'GET', 
     url: window.location.href, 
     data: { 
      'user_queryset': user_queryset 
     } 

    }) 
}); 

profile.html

<!--posts--> 
<div class="user_posts_div"> 
    <div class="endless_page_template"> 
     {% include "user_posts.html" %} 
    </div> 
</div> 

<!--comments--> 
<div class="user_comments_div"> 
    <div class="endless_page_template"> 
     {% include "user_comments.html" %} 
    </div> 
</div> 

user_posts.html (बच्चे टेम्पलेट)

{% paginate 5 user_posts %} 
    {% for post in user_posts %} 
     <div class="user_post"> 
      <p class="user_post_title_p"><a class="user_post_title" href="{% url 'article' category=post.entered_category id=post.id %}">{{ post.title }}</a></p> 
      <p class="user_post_category">/{{ post.entered_category }}</p> 
      <p class="user_post_date">{{ post.date|timesince }}</p> 
     </div> 

    {% endfor %} 
{% show_more 'more posts' '...' %} 

उत्तर

10

रेखा से नीचे के उत्पादन में क्या है?

print('Queryset:', user_queryset) 

मुझे लगता है कि आप नीचे दिए गए लाइन

user_queryset = request.GET.get('user_queryset') 

इस पोस्ट और टिप्पणी भाग की शर्त के साथ मैच के लिए ठीक से पता होना पैरामीटर मान नहीं लौटा रहा है में समस्या है।

+0

हाँ आप सही हैं, यह निश्चित रूप से है। किसी कारण से यह दो बार प्रिंट करता है, पहली बार मूल टेम्पलेट ('profile.html') को प्रिंट करना गलत है; इसे केवल बच्चे के टेम्पलेट को प्रिंट करना चाहिए ('user_posts.html') कोई विचार यह क्यों करेगा? मैंने अपने संपादन में आउटपुट जोड़ा है। – Zorgan

+0

दो बार कॉलिंग का मतलब है कि आप इस दृश्य को कॉल करने के लिए जेएस में कुछ गलत कर रहे हैं। आपने is_ajax() विधि से पहले पेज_ टेम्पलेट = 'profile.html' परिभाषित किया है। इसलिए जब आपको user_queryset में कोई नहीं मिला, तो इसकी प्रिंट प्रोफ़ाइल.html टेम्पलेट –

+0

मैंने संपादन में अपना जेएस कोड जोड़ा है। इसमें कुछ भी गलत नहीं लगता है; यह क्लिक किए गए क्लिक के आधार पर सही शब्द लॉग करता है। कोई उपाय? – Zorgan

1

आप के पास अपने जावास्क्रिप्ट में जाँच की जा सकी:

var user_queryset = 'user_comments'; 

मुझे लगता है, अगर आप टिप्पणी करने के लिए सीधे जाने, कि चर user_queryset अपरिभाषित किया जाएगा और:

user_queryset = 'user_comments'; 

कोशिश करने के लिए इसे बदलने के लिए यह 'user_comments' के रूप में पारित नहीं होता है।

+0

हाँ मैंने कोशिश की है और किसी भी चीज को ठीक नहीं करता है। मैंने अभी अपना जेएस संपादित किया है जो दिखाता है कि मैं पहले 'user_queryset' चर घोषित करता हूं, इसलिए यह अपरिभाषित नहीं है। जेएस सही डेटा भेजता है, समस्या विचारों में प्रतीत होती है। – Zorgan

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