简体   繁体   English

Django混合了我的模板

[英]Django mixes my templates

System: Debian Wheezy, Django 1.5 系统:Debian Wheezy,Django 1.5

Hello everyone, 大家好,

I am pretty new to Django and I have already encountered a problem that I can't find the solution for. 我是Django的新手,我已经遇到了无法找到解决方案的问题。 Moreover, I have a little project with 4 or 5 apps in it and I am trying to navigate from one app to another. 此外,我有一个包含4个或5个应用程序的小项目,我正尝试从一个应用程序导航到另一个应用程序。 Here is what I am trying to do - I have a mainMenu app, which is loaded when you go to the localhost. 这是我要尝试执行的操作-我有一个mainMenu应用程序,当您转到本地主机时会加载该应用程序。 From this app's template I navigate to other apps like that: 从该应用程序的模板中,我导航至其他类似的应用程序:

<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>

Now, when you go to, Glossary, for example... I render some glossary.html with some context but what happens is that... you get the look of the glossary.html in the browser... but the actual code is from the mainMenu page. 现在,例如,当您进入词汇表时...我渲染了一些上下文相关的lossary.html,但是发生的是...您在浏览器中看到了lossary.html的外观...但是实际的代码来自mainMenu页面。 Here is the view that renders the glossary: 这是呈现词汇表的视图:

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

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

Note that if you reload the page in the browser, you get the actual glossary.html, rendered properly. 请注意,如果在浏览器中重新加载页面,则会获得正确呈现的实际glossary.html。 The problem is not in the rendering itself, as I get the information that I need into the template... the problem is that, for some reason, it does not load the template properly because I have some javascript functions in it... but what I get is the javascript functions from the mainMenu page. 问题不在于渲染本身,因为我在模板中获得了所需的信息...问题是由于某种原因,由于我具有一些javascript函数,因此无法正确加载模板...但是我得到的是来自mainMenu页面的javascript函数。 In a way, it mixes the old and the new template. 在某种程度上,它混合了旧模板和新模板。

Here is the urls.py for the glossary page: 这是词汇表页面的urls.py:

from django.conf.urls import patterns, url

from glossary import views

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

That is all I can think of right now, please if you require more information, let me know. 这就是我现在能想到的,如果您需要更多信息,请告诉我。

---> EDIT <--- --->编辑<---

Ok, here is the full data structure that I pass to the glossary.html 好的,这是我传递给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)

and the java script function is just a function that queries this data structure... but the problem is that... my function does not appear in the code of the template after the rendering... the only javascript functions that appear are the ones from the old template. Java脚本函数只是查询此数据结构的函数...但是问题是...我的函数在渲染后没有出现在模板的代码中...唯一出现的JavaScript函数是那些来自旧模板。 However, when I refresh the page... everything is fine. 但是,当我刷新页面时...一切都很好。 I mean... I get the template... but it is mixed with the old one... I don't think that the javascript is the problem here. 我的意思是...我得到了模板...但它与旧模板混合在一起...我不认为javascript是这里的问题。

---> EDIT 2 <--- --->编辑2 <---

The project's urls.py: 该项目的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!
)

Glossary's urls.py: 词汇表的urls.py:

from django.conf.urls import patterns, url

from glossary import views

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

Look at the admin line in your urls: 在您的网址中查看管理行:

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

Now look at glossary: 现在来看词汇表:

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

See that? 看到了吗? The $ sign means the URL ends there. $符号表示URL 到此为止 So any 'child'-urls would not be caught by it. 因此,任何“儿童”网址都不会被它捕获。 What you want it to be is like the admin is: 您希望它像管理员一样:

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

edit 编辑

As we discussed in the comments, the problem hid inside your javascript code for the page, which instead of redirecting fetched the data and filled it inside a div. 正如我们在评论中讨论的那样,问题隐藏在页面的javascript代码内部,而不是重定向获取数据并将其填充到div中。 I can't exactly tell why that would happen. 我无法确切地说出为什么会发生。 You should probably go over your code and look for something along these lines: 您可能应该遍历代码,并按照以下方式寻找内容:

$('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)
    });

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

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