【问题标题】:Django mixes my templatesDjango 混合我的模板
【发布时间】:2013-12-31 04:06:23
【问题描述】:

系统:Debian Wheezy、Django 1.5

大家好,

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

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

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

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

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

请注意,如果您在浏览器中重新加载页面,您将获得正确呈现的实际词汇表.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'),
)

我现在能想到的就这么多,如果您需要更多信息,请告诉我。

---> 编辑

好的,这是我传递给glossary.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'),
)

【问题讨论】:

  • 这里的两个可疑之处是“我使用的一些数据结构”和“一些 javascript 函数”。我认为你需要提供更多关于这些的细节。
  • 我添加了一些说明。但是,我不认为 jacascript 和数据结构是问题,因为我在词汇表模板中获得了我想要的信息 - 问题在于它将词汇表的模板与前一页的模板混合了一些原因。
  • 似乎词汇表的链接(即a href="/glossary/")与urls.py 中的链接(即url(r'^$')不匹配。除非我在这里遗漏了什么?
  • 嗯...我不太了解Django...但是我认为url是正确的,原因如下,如果我错了,请纠正我:url(r'^$s ) 说“如果我请求了词汇表应用程序,请打开此视图” - 因为这是词汇表应用程序的 urls.py。我有一个不同的 urls.py 项目,其中包含不同应用程序的不同 urls.py。
  • 如果我输入其他内容,例如url(r'^newpage$'),这是否意味着在浏览器中,我可以使用以下链接转到新页面:127.0.0.1:8000/glossary/newpage 而我的 url url(r'^$') 说如果我请求转到:127.0.0.1:8000/glossary 我会得到一些与该 url 相关联的视图。我说的对吗?

标签: javascript python django templates mixing


【解决方案1】:

查看您网址中的管理行:

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

现在看看词汇表:

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

看到了吗? $ 符号表示 URL 结束。所以任何“孩子”的网址都不会被它抓住。你想要的就像管理员一样:

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

编辑

正如我们在 cmets 中所讨论的,问题隐藏在页面的 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)
    });

【讨论】:

  • 我已经改过了,但问题仍然存在......它混淆了模板
  • 您是否使用了扩展块?另外,如果你完全省略 mainManu urls 行会发生什么?只需评论除词汇表之外的所有内容。然后会发生什么?
  • 不,我不使用扩展。无论如何...如果我注释掉除词汇表之外的所有内容 - 我将只能访问 127.0.0.1:8000/glossary。问题是,如果我直接使用其链接访问该页面 - 它工作正常。不过,如果我从另一个页面加载它......就会出现问题。
  • 我一直在使用 Chrome 的“检查元素”,我注意到以下事情......每当我从 MainPage 导航到 Glossary 时......它只会添加一个新的 'div ' 进入带有新上下文的当前页面。就好像我正在使用 JQuery 并且我在同一个 HTML 文件中有 2 个页面。好吧,我使用 JQuery...但是我有指向新页面的正常链接...而不是指向同一页面中的某个位置。
  • 等等...我很困惑。 “从另一个页面加载”是什么意思?为什么重要的是您来自哪个链接?你在用你的缓存做一些奇怪的事情吗?你在使用 AJAX 吗?
猜你喜欢
  • 2015-04-28
  • 2012-02-25
  • 2021-04-03
  • 1970-01-01
  • 2011-05-17
  • 2021-12-24
  • 1970-01-01
  • 2019-12-15
  • 2010-12-01
相关资源
最近更新 更多