60127

Django: Migrating from MEDIA_URL to STATIC_URL

Question:

Since Django 1.3 the concept of STATIC_URL has been introduced to separate use media files from css and js files.

I have set my STATIC_ROOT = '/home/user/project/static/' and STATIC_URL = '/static/'.

In my base.html, i have changed the path like this:

<link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet">

And in url.py I have added the following two lines:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns ... urlpatterns += staticfiles_urlpatterns()

My view renders the template with RequestCOntext:

return render(request, 'main_page.html', variables)

But in the development I still get 404 when running runserver.

[18/Aug/2012 17:12:04] "GET /static/jquery/jquery-1.8.0.min.js HTTP/1.1" 404 1682

What could I be missing?

Answer1:

settings.py

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) MEDIA_ROOT = os.path.join(PROJECT_DIR, 'site_media') MEDIA_URL = '/site_media/' STATIC_URL = '/static/' if DEBUG: STATIC_ROOT = os.path.join(PROJECT_DIR, 'static') else: STATICFILES_DIRS = ( os.path.join(PROJECT_DIR, 'static'), )

urls.py

if settings.DEBUG: urlpatterns += patterns('', url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), )

That should work :)

Recommend

  • Using target on div without a href
  • How to display the hotel names that have the maximum count from data base by calculating sum in PHP?
  • how to add semantic ui in a rails app?
  • NetBeans doesn't see style.css [duplicate]
  • Jquery Show & ScrollTop (or ScrollTo)
  • Bootstrap Popover showing at wrong place upon zoom in/out or resizing Browser window
  • How to add regEx in angular filter
  • Angular Bootstrap Carousel Slide Transition not working correctly
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Simulate click Geckofx vb,net
  • How to view images from protected folder with php?
  • D3 get axis values on zoom event
  • Adjust width of select element according to selected option's width
  • Webgrid not refreshing after delete MVC
  • Custom validator control occupying space even though display set to dynamic
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • JSON response opens as a file, but I can't access it with JavaScript
  • Jquery UI tool tip close icon
  • HTML download movie download link
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Circular dependency while pushing http interceptor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Traverse Array and Display in markup
  • bootstrap to use multiple ng-app
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal