0

Facing issues for loading (including) dynamic content through a template tag "include" or from 'Ajax' based Loading.

As said Here, There are three ways to do... But still having issues

Working on Django 2.1.3, Python 3.6...

I have tried two ways: 1. Using "Include" Tag 2. By Loading Through Ajax

Python

views.py
def equity(request,symbol):

    """
    some code
    """

    context_all = {
    'template_Name': ('/autoRefreshEqu/{}/').format(symbol)
        }
    template = loader.get_template('equity/equity.html')
    response_body = template.render(context_all,request)
    return HttpResponse(response_body)

def autoRefreshEqu(request,symbol):

    """
    code for generating context
    """

    context_all = {
    'quote':quote,
    'changeQuote':changeQuote,
    'lastPrice': x['data'][0]['change'],
    'change':x['data'][0]['change'],
    'pChange':x['data'][0]['pChange'],
    'lastUpdateTime': x['lastUpdateTime'],

    }

template = loader.get_template('equity/autorefequ.html')
response_body = template.render(context_all,request)
return HttpResponse(response_body)
urls.py
path('autoRefreshEqu/<slug:symbol>/',views.autoRefreshEqu, name="autoRefreshEqu"),

path('equity/<slug:symbol>/',views.equity, name="equity"),

HTML Section

equity/equity.html

Reference Provided for Comment With Nos. as Guidance in Description.

<!-- Code Persists + Load Static + Humanize + Extends base.html, all working well -->

<!-- Reference for 2. -->
<script>$("#autoupdate").load("{% url template_Name %}");</script>

<div id="autoupdate">

<!-- Reference for 1. -->
{include template_Name} 

<!-- Load The Content Generated by autoRefreshEqu/<slug:symbol>/ -->
</div>
equity/autorefequ.html
{% load humanize %}
{% load static %}
<div class="card text-center">
<div class="card-header">
    <h2 class="alignleft">Current Price</h2>
    <p class="alignright"><label class="switch"> <input type="checkbox" > <span class="slider"></span></label></p>
    <!--p class="alignright"><label class="switch"> <input type="checkbox" checked> <span class="slider"></span></label></p-->
</div>
<div class="card-body">
  <!--h5 class="card-title"></h5-->
  <p class="card-text">
        <table class="table table-bordered">
                {% if changeQuote > 0 %}
                      <td class="m" rowspan="2" style="color:green">
                         <strong style="font-size: 40px;">
                            <center>₹ {{lastPrice |intcomma}} </center>
                         </strong> 
                         <center><img src="{% static "/images/arrow_ticker_up.gif" %}">&nbsp;₹ {{change |intcomma}} ({{pChange |intcomma}} %)</center>
                      </td>
                      {% else %}
                      <td class="m" rowspan="2" style="color:red">
                         <strong style="font-size: 40px;">
                            <center>₹ {{lastPrice |intcomma}} </center>
                         </strong>
                         <center><img src="{% static "/images/arrow_ticker_down.gif" %}">&nbsp;₹ {{change |intcomma}} ({{pChange |intcomma}} %)</center>
                      </td>
                      {% endif %}
             </table>
</div>
<div class="card-footer text-muted">
    <p class="alignleft">Last Updated at {{lastUpdateTime}} <p class="alignright"> <span class="reload"><a href="#">↻</a></span></p>
</div>

Traceback

[05/Feb/2019 14:07:56] "GET /quoteAPI/v1/EICHERMOT HTTP/1.1" 301 0
[05/Feb/2019 14:07:58] "GET /quoteAPI/v1/EICHERMOT/ HTTP/1.1" 200 1903
[05/Feb/2019 14:07:58] "GET /autoRefreshEqu/EICHERMOT/ HTTP/1.1" 200 792
[05/Feb/2019 14:08:16] "GET /quoteAPI/v1/EICHERMOT HTTP/1.1" 301 0
[05/Feb/2019 14:08:18] "GET /quoteAPI/v1/EICHERMOT/ HTTP/1.1" 200 1903
[05/Feb/2019 14:08:18] "GET /quoteAPI/v1/EICHERMOT HTTP/1.1" 301 0
[05/Feb/2019 14:08:20] "GET /quoteAPI/v1/EICHERMOT/ HTTP/1.1" 200 1903
Internal Server Error: /equity/EICHERMOT/
Traceback (most recent call last):
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "C:\Users\localhost\Desktop\New F 3\project\frontend\ticker\views.py", line 119, in equity
response_body = template.render(context_all,request)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
  File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\template\defaulttags.py", line 442, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\urls\base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
 File "C:\Users\localhost\Desktop\New F 3\project\lib\site-packages\django\urls\resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for '/autoRefreshEqu/EICHERMOT/' not found. '/autoRefreshEqu/EICHERMOT/' is not a valid view function or pattern name.

Suppose, I'm Calling URL as

/equity/EICHERMOT/

then the template should call out

/autoRefreshEqu/EICHERMOT/

and render to the main equity template.

The div having id=autoupdate should have all the context generated by autoRefreshEqu.

Thanks.

1 Answer 1

1

I'm not sure why you're passing the URL through the {% url %} tag. That's for taking a URL pattern name and converting it to a URL. You already have a fully-formed URL, so you can just use it directly:

<script>$("#autoupdate").load("{{ template_Name }}");</script>

Of course, that doesn't make this a good way to do things. You should be using {% url %} rather than hard-coding your URL in the view. Remove that template_Name variable and instead pass symbol; then, in the template:

<script>$("#autoupdate").load("{% url "autoRefreshEqu" symbol=symbol %}");</script>
Sign up to request clarification or add additional context in comments.

2 Comments

It Works, but the content it should load isn't showing up.
<script> $.ajax({ url: "{% url 'autoRefreshEqu' symbol=symbol %}", success: function(data) { $('#autoupdate').html(data); } }); </script> : Is this method ok to work with ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.