1

System: 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. Moreover, I have a little project with 4 or 5 apps in it and I am trying to navigate from one app to another. Here is what I am trying to do - I have a mainMenu app, which is loaded when you go to the localhost. 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. 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. 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. In a way, it mixes the old and the new template.

Here is the urls.py for the glossary page:

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

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

---> EDIT 2 <---

The project's 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:

from django.conf.urls import patterns, url

from glossary import views

urlpatterns = patterns('',
    url(r'^$', views.search_gloss, name='search_gloss'),
)
Rincer
  • 23
  • 1
  • 6
  • 1
    The two suspicious things here are "some data structure that I use" and "some javascript functions". I think you need to give a bit more detail about those. – Daniel Roseman Dec 12 '13 at 16:26
  • I've added some clarifications. However, I don't think that the jacascript and the data structure are the problem since I get the information that I want into the glossary template - the problem is that it mixes the template of the glossary with the template of the previous page for some reason. – Rincer Dec 12 '13 at 16:50
  • It seems the link to glossary (i.e. `a href="/glossary/"`) does not match the one in urls.py (i.e. `url(r'^$'`). Unless I'm missing something here? – yuvi Dec 12 '13 at 17:21
  • Hm... I don't really understand Django... but I think that the url is correct for the following reason, and correct me if I am wrong: url(r'^$s) says "open this view, if I have requested the Glossary app" - since this is the urls.py for the glossary app. I have a different urls.py for the project where I include the different urls.py for the different apps. – Rincer Dec 12 '13 at 17:42
  • if I put something else, like `url(r'^newpage$')`, wouldn't this mean that in the browser, I can go to new page with the following link: 127.0.0.1:8000/glossary/newpage while my url `url(r'^$')` says that If I request to go to: 127.0.0.1:8000/glossary I get some view that I have associated with that url. Am I correct? – Rincer Dec 12 '13 at 17:47
  • `127.0.0.1:8000/glossary` doesn't automtically go to an application called glossary, you have to tell it somewhere to do so. Your url configuration there will simply go to `127.0.0.1:8000/` and will probably catch any other request to any url the follows (becuase it's the first match). That's probably why you're seeing that mixup – yuvi Dec 12 '13 at 17:57
  • Also, if you're new to django, don't start by building 4-5 apps. Start by [going over the tutorial](https://docs.djangoproject.com/en/1.6/intro/tutorial01/) – yuvi Dec 12 '13 at 17:58
  • I went through that tutorial already. I understand what you are saying, but maybe I am not explaining myself correctly. I have a urls.py for the whole project where I include the urls.py for the other apps... there I handle calls to 127.0.0.1:8000/glossary... I will update the question, so you can check it. – Rincer Dec 12 '13 at 18:07
  • But glossary/ isn't mapped to the glossary app, only glossary is -- you don't have the / in the urls.py, but you do have a $ meaning that the url must end after glossary. Better put a / in there. – RemcoGerlich Dec 12 '13 at 18:27

1 Answers1

1

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. 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. 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)
    });
yuvi
  • 18,155
  • 8
  • 56
  • 93
  • I've changed it, but the problem is still there... it mixes up the templates – Rincer Dec 12 '13 at 22:33
  • Are you using an extend block maybe? Also, what happens if you ommit the mainManu urls line completely? Just comment everything except for glossary. What happens then? – yuvi Dec 13 '13 at 11:30
  • Nope, I don't use extend. Anyway... if I comment out everything except for the glossary - I will be able to only access 127.0.0.1:8000/glossary. The thing is that, if I access the page directly with its link - it works fine. Nevertheless, if I load it from another page... the problem occurs. – Rincer Dec 13 '13 at 14:47
  • I've been playing a little bit with Chrome's "Inspect Element" and I've noticed the following thing... whenever I navigate from MainPage to Glossary... it only adds a new 'div' into the current page with the new context. It's just as if I am using JQuery and I have 2 pages in the same HTML file. Well, I use JQuery... but I have normal links that point to the new page...not to a place in the same page. – Rincer Dec 13 '13 at 16:26
  • Wait... I'm confused. What does "load it from another page" mean? Why does it matter what link you're coming from? Are you doing something weird with your cache? Are you using AJAX? – yuvi Dec 13 '13 at 16:37
  • Ok, here is what happens... if you directly type the link for the glossary page into the browser... it loads everything as it is supposed to. Now, if you load the MainPage and then click the link for the glossary - it sends you to the glossary page but what it really does (from what i've seen) is to only add the glossary as a div to the current template. I am not even touching the cache... and I use JQuery mobile. – Rincer Dec 13 '13 at 16:48
  • I have also noticed one more thing... I've tried to print the result after the rendering is done into the terminal and the rendering is fine... it loads the correct template and puts in it the correct information... however that is not what I get in the browser when I check it with "Inspect Element" – Rincer Dec 13 '13 at 16:50
  • What you're describing is weird as hell. I can't think of any situation that will cause it. Remove any javascript you have on any of the pages, what happens now? – yuvi Dec 13 '13 at 20:06
  • Yep, I just did that and it is even weirder :D When I comment the JQuery stuff... it works fine... it uses the correct template and I can see my custom javascript functions. – Rincer Dec 13 '13 at 20:44
  • For some reason, the JQuery decides that it has to do everything in the same page. It adds another div-page instead of just navigating to the new HTML file that I pass with Django. – Rincer Dec 13 '13 at 20:52
  • Ok... I understand what the problem is now... I had to tell the JQuery that these links were pointing to external files, not to a location on the same page. The fix is to put `rel="external"` in the `` tag. THANK YOU A LOT FOR YOUR HELP!!! – Rincer Dec 13 '13 at 21:08
  • Sure thing, though I'm still puzzled as to why your jQuery would do anything like that without you actively telling it to (maybe you're using some plugin?) – yuvi Dec 13 '13 at 21:44
  • Nope... no plugins... I guess it's just the default thing to do when you redirect to another page – Rincer Dec 14 '13 at 02:53
  • No it isn't. It's absolutely not the default. – yuvi Dec 14 '13 at 09:22
  • Well, if that is the case... I don't know then :D By the way, if you want, you can write a new answer, so I can accept it. – Rincer Dec 14 '13 at 16:01
  • I just edited it to my original answer. I also added an example of how to create the effect you described using jQuery. Look for it in your js functions – yuvi Dec 14 '13 at 18:40