防止在Django中更新上下文

时间:2018-07-02 12:39:19

标签: django

所以我对django的知识很少,这使得很难确切地解释正在发生的事情。我认为,具体而言,关于样板/项目/框架如何设置的知识对我来说有点困难解释这个问题,但我会尝试的

我正在尝试使用给定的上下文呈现Django模板。现在,我设法渲染模板,但是上下文不断更新。如何使模板区分上下文?或如何阻止上下文进行自我更新?

    def render_stateTree(self,request,temp_context):
        c = RequestContext(request, temp_context)
        # Create a response
        response = TemplateResponse(request, 'admin/StateTree.html', c)
        # Register the callback
        response.resolve_context(temp_context)
        # Return the response
        return response


    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 = {'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')
        urlpatterns += patterns('',url(r''+filename, lambda x : self.render_stateTree(x, c)))+static(settings.STATIC_URL, document_root=settings.STATIC_URL+'stateTreeDocs')**

        #print context_txt[filename]['textfile']


    return urlpatterns

0 个答案:

没有答案