djangostaticurlconf

Static files being referenced via different path in same Django view


I recently updated one of my views and URLs to take an additional parameter and now that I have done this the page which is rendered displays with no css or javascript because the static path is wrong.

I am obviously not understanding correctly how static is served however I didn't think that changing the URL confs would change the path that Django would look for static. All my static for the whole site is in the one place.

urls.py

url(r'^fill/(.*)/(.*)/$', views.fill_survey, name='unique_getfile'),
url(r'^fill/(.*)', views.fill_survey, name='unique_getfile'),

and here is my view ... first block executes for the first url match and the second block executes for the bottom url

def fill_survey(request, unique_url, new_name="blank"):
    """
    get file or redirect to error page
    """
    if len(unique_url) < 50:
        if unique_url == "new":
            firstlast = new_name.split(" ")
            c = Client.objects.get(firstname=firstlast[0], lastname=firstlast[1])
            survey_url_number = generate_survey(request, c.email)
            response = Survey.objects.get(number=survey_url_number['number'])
            return render(request, 'survey/survey.html', {'survey': response})
        else:
            response = Survey.objects.get(number=unique_url)
            return render(request, 'survey/survey.html', {'survey': response})

The rendered page then has the following static paths for the first and second url match respectively:

wwww.mysite.com/static/etc...
www.mysite.com/module_name/static/etc...

Paths in my template like:

<link rel="stylesheet" href="../../../static/classic/global/css/bootstrap.min.css">

Why are these different URLs leading to different static paths?

Thanks in advance!


Solution

  • Here's the relevant docs but by default Django only looks for static resources in /static/ folder or its subfolders. You can define a list of static file directories in your settings file if you want though.

    https://docs.djangoproject.com/en/2.0/howto/static-files/

    EDIT: Response to comment. Can I see your template? Also, I'd try changing all your urls to end in a /$.

    EDIT: Response to posted template. I use the static tag rather than the absolute. I.E.

    <link rel="stylesheet" href="{% static "/css/bootstrap-3.3.7.min.css" %}">

    This way if we mess with the location of our templates we don't ned to go back and change all the templates .

    A couple thought about your html:

    Regarding why I end my addresses in /$. Here's a good answer. Why django urls end with a slash?

    I wish I had a clearer answer for you but try the trailing / in the url and the leading / or {% static %} in your template.