通过模板标签“ include”或基于“ Ajax”的加载来加载(包括)动态内容时遇到的问题。
如Here所述,有三种方法可以做...但是仍然有问题
使用Django 2.1.3,Python 3.6 ...
我尝试了两种方法: 1.使用“包含”标签 2.通过Ajax加载
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"),
为注释提供的参考,以编号作为说明中的指导。
<!-- 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" %}"> ₹ {{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" %}"> ₹ {{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>
[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.
假设我将URL称为
/ equity / EICHERMOT /
然后模板应调出
/ autoRefreshEqu / EICHERMOT /
并渲染到主要资产模板。
具有id = autoupdate的div应该具有由autoRefreshEqu生成的所有上下文。
谢谢。
答案 0 :(得分:1)
我不确定您为什么要通过{% url %}
标记传递URL。这是为了获取URL模式名称并将其转换为URL。您已经有一个完整的URL,因此您可以直接使用它:
<script>$("#autoupdate").load("{{ template_Name }}");</script>
当然,这并不是做事情的好方法。您应该使用{% url %}
,而不是在视图中硬编码URL。删除该template_Name
变量,然后传递symbol
;然后,在模板中:
<script>$("#autoupdate").load("{% url "autoRefreshEqu" symbol=symbol %}");</script>