Amit Pal
Amit Pal

Reputation: 11042

How to use Django views and template tag simultanosly?

In my Django-template:

<div class="A">
    {% url renders_data object.id %}
</div>
<div class="B">
    {% render_data object.id %}
</div>

Div A is common way to call a method in views.py whereas Div B is for template tags.

User will open a link. Let's say: /myapp/test/ a page will open contain two template tag section at the page. Name of this tag is render_data I want to load the data into each template tag with Ajax. To work with it we need request.ajax:. That is why i thought to write views method. I thought to complete it with the following way:

  • I wrote the exact copy of template tag in views.py (renders_data with passing object_id parameter) and render it to the right template. When i open the maypp/test after removing the div A from template. It shows the URL (myapp/test/<object_id>) in each template tags section (each corner) except the data. Is their any possibility to show the context except this URL See the image when i choose this option
  • Second i also thought to import views method (renders_data) in template tag (render_data). So that data will display in each corner and request.Ajax: will also work. if this can be possible then how?
  • I am not able to solve this issue. please help me :(

    See how the render_data looks like:

     @register.simple_tag
     def render_widget(object_id):
         from myapp.plugins.model import Widgetsetting
         widget_setting = Widetsetting.objects.get(id = object_id)
         widget = widget_settings.get_widget()
         template_name = widget.template_name
         context = widget.context(widget=widget_settings)
         t =  get_template("widgets/%s" % template_name)
         return t.render(Context(context))
    

    Upvotes: 0

    Views: 2516

    Answers (1)

    Anentropic
    Anentropic

    Reputation: 33823

    From the code you've posted something like below should work...

    in views.py:

    from django.http import HttpResponse
    from netadmin.plugins.templatetags.widgets import render_widget
    
    def ajax_render_data(request, object_id):
        return HttpResponse(render_widget(object_id))
    

    in your django template:
    (you said you want several of these on the page, so I'm going to use a loop in the django template, I don't know what your widget collection is called but I'm sure you can work it out for your particular case)

    <div class="widgets-container">
        {% for widget_settings in widgetsettings.objects.all %}
        <div class="widget-content" id="widget-content-{{ widget_settings.pk }}">
            not loaded yet
        </div>
        {% endfor %}
    </div>
    
    <script>
    // here we use django to render the ajax urls into an object
    // in javascript so we can use it on the client side 
    var ajax_urls = {
      {% for widget_settings in widgetsettings.objects.all %}
        "{{ widget_settings.pk }}: "{% url ajax_render_data widget_settings.pk %}"{% if not forloop.last %},{% endif %}
      {% endfor %}
    };
    
    // (I'll assume you're using jQuery)
    // start the ajax calls when the page is loaded:
    $(document).ready(loadWidgets());
    
    function loadWidgets() {
        // loop over the urls we provided from django:
        jQuery.each(ajax_urls, function(widget_id, url) {
            // do the ajax call:
            $.get(url, function(data) {
                // put the content into the widget div:
                $('#widget-content-'+widget_id).html(data);
            });
        });
    }
    </script>
    

    in urls.py:

    urlpatterns += patterns('netadmin.plugins.ajax_view',
        url(r'^ajax/(?P<object_id>\d+)/$', 'ajax_render_data', name='ajax_render_data'),
    )
    

    Upvotes: 1

    Related Questions