Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add css and jquery file in my django project?

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

like image 411
mushfiq Avatar asked Nov 20 '10 08:11

mushfiq


People also ask

Where do I put CSS in Django?

If you follow django's guidelines, you can simplify your life greatly. In your sample code, inside your application directory, create a folder called static. Inside this folder, place your css files. Make sure you add myapp to the INSTALLED_APPS list in settings.py .

Can I use jQuery with Django?

Inside your settings.py file make sure that django. contrib. staticfiles is under INSTALLED_APPS (it is there by default). And now you can use jQuery throughout your site!

Can I use jQuery in CSS file?

Can I use jQuery in CSS file? The jQuery css() method is used to get the computed value of a CSS property or set one or more CSS properties for the selected elements.


2 Answers

The correct syntax is in the list of things you tried unsuccessfully:

<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}test.css" />

If you define your MEDIA_URL as "/media/", then that link will work out to be /media/test.css.

Provided that you have the following directory structure:

my_project
    |-- settings.py
    |-- urls.py
    |-- media
        |-- test.css

I would double check all your file and directory names, make sure you don't have any errant/extra slashes, etc.

Also, I presume that "test.css" was supposed to be "screen.css" like it was in all your other examples...

But basically, using an absolute url path (starting with the slash to indicate it resolves from the site root) will work just the same as using a relative path (../) as long as you actually have your files in the right place. Then what you have will work.

like image 175
Gabriel Hurley Avatar answered Sep 22 '22 20:09

Gabriel Hurley


My dir structure is as follows:

|-- test_form
|-- /settings.py
|-- /urls.py
|-- /media
|   '-- test.css
'-- /templates
    '-- ...

And I added the following syntax in my template.html file:

<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}test.css" />

I am confused where the problem is.

like image 33
2 revs, 2 users 88% Avatar answered Sep 25 '22 20:09

2 revs, 2 users 88%