简体   繁体   English

Django-加载动态模板

[英]Django - Loading dynamic Templates

I would like to know information refer to the loading files in Django. 我想知道有关Django中加载文件的信息。 First of all, it's pretty clear to load static files. 首先,很明显要加载静态文件。 So in my case, we have simple website, where one of the script generate dynamic content. 因此,以我为例,我们有一个简单的网站,其中一个脚本生成动态内容。 That dynamic content, it's: 该动态内容为:

  • List of folders, 文件夹列表,
  • With one HTML file 带有一个HTML文件
  • And JPG Images. 和JPG图片。

And main tasks here - it's loading that HTML files, with images. 这里的主要任务是-加载带有图像的HTML文件。


That generated content, might be changed, as much, as I need. 生成的内容可能会根据我的需要进行更改。 And in my example, I can put That Contents into templates folder, and load like the snippet below. 在我的示例中,我可以将“内容”放入“ templates文件夹中,并像下面的代码片段一样加载。 But where all images are missing. 但是所有图像都丢失了。 So I need to put that HTML files to the templates folder in project, and images to the separated static folder, to load them. 因此,我需要将HTML文件放入项目中的templates文件夹中,并将图像放入单独的static文件夹中,以进行加载。

Problem here, that I want to locate Generated Source in some single and separated path, but keep possibility to load HTML, with images. 这里的问题是,我想在一些单独的路径中找到“生成的源”,但是要保留用图像加载HTML的可能性。

## Ex. including HTML page (which should located in templates)
    <div class="container">

        {% include "stories/Princess_Run_Story/Princess_Run_Story.html" %}

    </div>


## Ex. loading IMG file(which should located in static)
     <img class="first-slide" 
              src= "{% static '/workbook/images/galaxy_droid_1.jpg' %}" >

yes it will work, just you need to send dynamic image date from View while calling this snippent. 是的,它将起作用,只是您需要在调用此代码段时从View发送动态图像日期。 put following detail in proper path only. 仅将以下详细信息放在正确的路径中。

{% include "stories/Princess_Run_Story/Princess_Run_Story.html" %} {%包括“故事/Princess_Run_Story/Princess_Run_Story.html”%}

Alternate Option: Actually just call Ajax on click of button and prepare image date in snippet through backend and send snippet on Ajax success Like below code in javascript: 替代选项:实际上,只需单击按钮即可调用Ajax,并通过后端在代码段中准备图像日期,并在Ajax成功时发送代码段,例如javascript中的以下代码:

there $('.container').html(snippet_data)

in backend: 在后端:

render_data = {'image_data': image_data}
t = loader.get_template('stories/Princess_Run_Story/Princess_Run_Story.html')
html = t.render(RequestContext(request, render_data))
return HttpResponse(json.dumps({'html': html}), 'application/json')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM