在Django中将URL作为上下文传递

时间:2018-06-28 13:15:30

标签: django parsing url static

我对Django非常陌生。我想通过Django模板解析javascript中的txt文件。该文件被置于静态并被加载。现在,我只需要确保传递正确的名称即可。最好的方法是什么?

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Geoinfo SDE StateTree</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.css" type="text/css" />

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.4.0/papaparse.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.js"></script>
<script type="text/javascript">
//IMPORTANT TO INCLUDE THESE LINES OR STATIC FILES SUCH AS  WIL_sde_states.txt  WILL NOT LOAD 
{% load staticfiles %}

function reverse(s){
    return s.split("").reverse().join("");
}
var current_url = window.location.href
var txt_file = reverse(reverse(current_url).substr(0, reverse(current_url).indexOf('/'))); 
var doc_location = "{{textfile}}"
console.log(doc_location)
console.log(txt_file)
Papa.parse("{% static textfile %}", {
  download: true,
  complete: function(results) {
    var tree = results.data;
    var nodes = new vis.DataSet();
    var edges = new vis.DataSet();
    var levels = [];
    etc.........

我需要这个的原因是因为我想根据当前所在的URL渲染/创建不同的状态树。从我已经读过的内容来看,不可能将javascript变量传递给django静态标签,因此我也无法以这种方式进行操作。我尝试通过上下文传递它,但是下面粘贴的代码似乎会不断更新上下文,以便它具有迭代的最后一个循环。因此,换句话说,文件始终具有相同的名称。

    def get_urls(self):
    from django.conf.urls import patterns, url, include

    if settings.DEBUG:
        self.check_dependencies()

    def wrap(view, cacheable=False):
        def wrapper(*args, **kwargs):
            return self.admin_view(view, cacheable)(*args, **kwargs)
        return update_wrapper(wrapper, view)

    # Admin-site-wide views.
    urlpatterns = patterns('',
        url(r'^$',
            wrap(self.index),
            name='index'),
        url(r'^logout/$',
            wrap(self.logout),
            name='logout'),
        url(r'^password_change/$',
            wrap(self.password_change, cacheable=True),
            name='password_change'),
        url(r'^password_change/done/$',
            wrap(self.password_change_done, cacheable=True),
            name='password_change_done'),
        url(r'^jsi18n/$',
            wrap(self.i18n_javascript, cacheable=True),
            name='jsi18n'),
        url(r'^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$',
            wrap(contenttype_views.shortcut),
            name='view_on_site'),
        url(r'^(?P<app_label>\w+)/$',
            wrap(self.app_index),
            name='app_list')
    )


    # Add in each model's views.
    for model, model_admin in six.iteritems(self._registry):
        urlpatterns += patterns('',
            url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name),
                include(model_admin.urls))
        )
        #urlpatterns += patterns('',url(r'statetree', lambda x : render(x,'admin/StateTree.html')))+static(settings.STATIC_URL, document_root=settings.STATIC_URL)
        #urlpatterns += patterns('',url(r'WIL_sde_states.txt',lambda x : 5)+ 
        urlpatterns += patterns('',url(r'statetree', lambda x : TemplateResponse(x,'admin/StateTree.html', context, current_app=self.name)))+static(settings.STATIC_URL, document_root=settings.STATIC_URL+'stateTreeDocs')    



    state_tree_directory = os.path.join(os.path.dirname(os.path.dirname(__file__)),'admin','static','stateTreeDocs')


    for filename in os.listdir(state_tree_directory):
        # context_txt[filename] = {
        #     'textfile' : 'stateTreeDocs/'+filename
        # }
        c = Context({'textfile' : 'stateTreeDocs/'+filename})
        urlpatterns += patterns('',url(r''+filename, lambda x : TemplateResponse(x,'admin/StateTree.html', c, current_app=self.name)))+static(settings.STATIC_URL, document_root=settings.STATIC_URL+'stateTreeDocs')
        print context_txt[filename]['textfile']


    return urlpatterns

任何帮助将不胜感激

0 个答案:

没有答案