I am learning Django for one of my web projects.
Facing difficulties to append css,jquery file in my project.
The template is very simple and need not to use extends.Just one page form.
What I have done to declare my media file that:
In settings.py file:
Added path:
`import os
def path(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
`
Then added:
MEDIA_ROOT = path('media') #media is my folder where all the css,js file are
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/'
TEMPLATE_DIRS = (
path('templates')
In the urls.py file added:
from django.conf import settings
urlpatterns = patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root' : settings.MEDIA_ROOT }),
In the template file I have tried with all these types of declaration:
<script type="text/javascript" src="/media/jquery.min.js"></script>
<script type="text/javascript" src="/media/site.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="/media/screen.css" />
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}test.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../media/screen.css" />
But when I loaded the template file as simple html with :
<script type="text/javascript" src="../media/jquery.min.js"></script>
<script type="text/javascript" src="../media/site.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="../media/screen.css" />
That worked.But I need to integrate within my Django project.
Hope will get the navigation and solve it 🙂
Thanks
My dir structure is as follows:
And I added the following syntax in my template.html file:
I am confused where the problem is.