Reputation: 21
I'm using STATIC_URL in one of my templates and it works properly if I use one url and fails if I use another. In both cases, the exact same view is being run with the exact same template.
Template code:
<link href="{{ STATIC_URL }}css/form.css" type="text/css" rel="stylesheet" />
<!-- rest of page... -->
View code:
def myform(request):
if request.method == 'POST':
form = MyForm(request.POST, request.FILES)
obj = save_form(form)
if obj:
send_confirmation_email(obj)
return redirect('done-page')
else:
form = MyForm()
return render(request, 'template.html', {'form': form,})
Urls
# site urls.py
urlpatterns = patterns('',
url(r'^$', 'site.app.views.myform'),
url(r'^app/', include('site.app.urls'),
#...
)
# app urls.py
urlpatterns('site.app.views',
url(r'^$', 'myform'),
#...
)
The above is a snippet of my django site. If the page is accessed via "mydomain.com/" (the index of the site), STATIC_URL is rendered properly and the css file is linked properly. If the page is accessed via "mydomain.com/app/" (the index of app), STATIC_URL becomes blank and, hence no css, but the rest of the page renders properly.
I was under the impression that if the views and templates are the same, the url should be irrelevant. Am I missing something? What must I change to allow STATIC_URL to render properly regardless of the url used?
Upvotes: 2
Views: 358
Reputation: 21
I found the error. The strange behavior occurred because this was a copy of a previously built django site. I overlooked the first argument of urlpatterns in app/urls.py, leaving the code pointing to an older version. Unfortunately, that code lived on the same box and was on the python path. Thus, the mistake failed to cause an exception and, instead, sinisterly did something I wasn't expecting.
The actual scenario looked like this:
# site_copy urls.py
urlpatterns = patterns('',
url(r'^$', 'site_copy.app.views.myform'),
url(r'^app/', include('site_copy.app.urls'),
#...
)
# site_copy/app urls.py
urlpatterns('site.app.views',
url(r'^$', 'myform'),
#...
)
Thank you all for helping.
Upvotes: 0
Reputation: 48485
Just guessing here, but it could be because you're not passing a ContextObject
in your view. So to fix this, you'd do this:
from django.template import RequestContext
...
def myform(request):
...
return render(request, 'template.html', {'form': form,}, context_instance=RequestContext(request))
Otherwise the STATIC_URL
variable will not be passed to the template, and be blank, as you described. I also assume you already have this in your Context Processors setting:
django.core.context_processors.static
Upvotes: 2
Reputation: 239430
I find it hard to believe that STATIC_URL
"becomes blank". There's no rational reason for this unless the offending view response was cached from a time when STATIC_URL
literally had no value. If you are employing view or template caching make sure you delete the cache, or similar restart memcached (or other). If you're in development and using runserver
, restart runserver
.
The other more likely scenario based on when it does and doesn't work, is that your STATIC_URL
is not prefixed with a /
, i.e. it's just static/
. That would make the link to the CSS file /static/css/forms.css
on the homepage (as it should be), but it would be /app/static/css/forms.css
on the other page, which is obviously not correct.
Upvotes: 2