javascript - Django 混合了我的模板

标签 javascript python django templates mixing

系统:Debian Wheezy、Django 1.5

大家好,

我对 Django 还很陌生,我已经遇到了一个无法找到解决方案的问题。此外,我有一个包含 4 或 5 个应用程序的小项目,我正在尝试从一个应用程序导航到另一个应用程序。这就是我想要做的 - 我有一个 mainMenu 应用程序,当您转到本地主机时会加载该应用程序。从这个应用程序的模板中,我导航到其他类似的应用程序:

<div class="ui-grid-a">
        <div class="ui-block-a"><a href="/labs/" data-role="button">Labs</a></div>
        <div class="ui-block-b"><a href="/quiz/" data-role="button">Quiz</a></div>     
    </div>
    <div class="ui-grid-a">
        <div class="ui-block-a"><a href="/primer/" data-role="button">Primers</a></div>
        <div class="ui-block-b"><a href="/timer/" data-role="button">Timers</a></div>      
    </div>
    <div class="ui-grid-a">
        <div class="ui-block-a"><a href="#" data-role="button">Calculations</a></div>
        <div class="ui-block-b"><a href="/mapping/" data-role="button">Mapping</a></div>       
    </div>
    <div class="ui-grid-a">
        {#  <!-- {% url 'glos:index' %} --> #}
        <div class="ui-block-a"><a href="/glossary/" data-role="button">Glossary</a></div>
        <div class="ui-block-b"><a href="/videos/" data-role="button">Videos</a></div>     
    </div>
</div>

现在,例如,当您转到术语表时...我用一些上下文渲染了一些lossary.html,但发生的情况是...您在浏览器中看到了glossary.html的外观...但是实际代码来自主菜单页面。这是呈现词汇表的 View :

def search_gloss(request):
    the_list = [...] #some data structure that I use

    context = { "terms": the_list }
    return render(request, 'glossary/glossary.html', context)

请注意,如果您在浏览器中重新加载页面,您将获得正确呈现的实际lossary.html。问题不在于渲染本身,因为我将需要的信息放入模板中...问题是,由于某种原因,它无法正确加载模板,因为我在其中有一些 javascript 函数...但我得到的是 mainMenu 页面中的 javascript 函数。在某种程度上,它混合了新旧模板。

这是词汇表页面的 urls.py:

from django.conf.urls import patterns, url

from glossary import views

urlpatterns = patterns('',
    url(r'^$', views.search_gloss, name='search_gloss'),
)

目前我能想到的就是这些,如果您需要更多信息,请告诉我。

---> 编辑 <---

好的,这是我传递给lossary.html的完整数据结构

def search_gloss(request):
    the_list = [('Acrylamide gels','A polymer gel used for electrophoresis of DNA or protein\
                    to measure their sizes (in daltons for proteins, or in base pairs for DNA).\
                    See "Gel Electrophoresis". Acrylamide gels are especially useful for high resolution\
                    separations of DNA in the range of tens to hundreds of nucleotides in length.'), 

        ('Agarose gels','A polysaccharide gel used to measure the size of nucleic acids (in bases or base pairs).\
                 See "Gel Electrophoresis". This is the gel of choice for DNA or RNA in the range of thousands\
                 of bases in length, or even up to 1 megabase if you are using pulsed field gel electrophoresis.'),

        ('Genome',"The total DNA contained in each cell of an organism. Mammalian genomic DNA (including that of humans) \
               contains thousands of genes, including coding regions, 5' and 3' untranslated regions, introns, 5' and 3'\
               flanking DNA. Also present in the genome are structural segments such as telomeric and centromeric DNAs \
               and replication origins, and intergenic DNA."),

        ('Hybridization','The reaction by which the pairing of complementary strands of nucleic acid occurs. DNA is usually\
                  double-stranded, and when the strands are separated they will re-hybridize under the appropriate \
                  conditions. Hybrids can form between DNA-DNA, DNA-RNA or RNA-RNA. They can form between a short \
                  strand and a long strand containing a region complementary to the short one. Imperfect hybrids can \
                  also form, but the more imperfect they are, the less stable they will be (and the less likely to form).\
                  To "anneal" two strands is the same as to "hybridize" them.')]

    context = { "terms": the_list }
    return render(request, 'glossary/glossary.html', context)

而java脚本函数只是查询这个数据结构的函数...但问题是...我的函数在渲染后没有出现在模板的代码中...唯一的javascript函数出现的是旧模板中的内容。然而,当我刷新页面时......一切都很好。我的意思是...我得到了模板...但它与旧模板混合在一起...我不认为 javascript 是这里的问题。

---> 编辑 2 <---

项目的urls.py:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    #url(r'^$', 'index.html', name='index'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

    # include the rango urls
    url(r'^login$', include('login.urls', namespace="log")),
    url(r'^quiz$', include('quiz.urls', namespace="quizes")),
    url(r'^glossary$', include('glossary.urls', namespace="glos")),
    url(r'^$', include('login.urls', namespace="log")),
    url(r'^mapping$', include('mapping.urls')), # ADD THIS NEW TUPLE!
    url(r'^main$', include('mainMenu.urls', namespace="main")), # ADD THIS NEW TUPLE!
    url(r'^timer$', include('timer.urls')), # ADD THIS NEW TUPLE!
    url(r'^primer$', include('primer.urls')), # ADD THIS NEW TUPLE!
    url(r'^labs$', include('labs.urls')), # ADD THIS NEW TUPLE!
    url(r'^videos$', include('videos.urls')), # ADD THIS NEW TUPLE!
)

词汇表的 urls.py:

from django.conf.urls import patterns, url

from glossary import views

urlpatterns = patterns('',
    url(r'^$', views.search_gloss, name='search_gloss'),
)

最佳答案

查看网址中的管理行:

r'^admin/', include(admin.site.urls)

现在看一下术语表:

r'^glossary$', include('glossary.urls')

看到了吗? $ 符号表示 URL 结束。因此任何“子”url 都不会被它捕获。你想要的就像管理员一样:

r'^glossary/', include('glossary.urls')

编辑

正如我们在评论中讨论的那样,问题隐藏在页面的 javascript 代码中,该代码不是重定向而是获取数据并将其填充到 div 中。我无法确切地说出为什么会发生这种情况。您可能应该检查您的代码并寻找以下内容:

$('a').on('click', function(e) {
        e.preventDefault(); 
        var body = $('body'),
              div = $('<div></div>')
              link = $(this).attr('href');
        data = $.load(link);
        div.html(data);
        div.appendTo(body)
    });

关于javascript - Django 混合了我的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20548701/

相关文章:

python - 达到 Django Celery 最大数据库连接数

python - Django key 泄露

python - DRF TypeError 'type' 对象不可迭代

javascript - React Native Object.freeze 不起作用

javascript - 如果 Observable 完成,我是否需要取消订阅 Observable?

python - django 表单将 css 添加到 ModelMultipleChoiceFIeld

python - 如何创建允许 list[][] 索引(2D)的 __getitem__ 方法

javascript - 根据文本字段输入的数据更改窗口的背景颜色

javascript - 从由 Array 组成的对象中获取值

python - Python 中的条件函数链