Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use local bootstrap? #350

Closed
cl0ne opened this issue Dec 4, 2016 · 14 comments
Closed

How to use local bootstrap? #350

cl0ne opened this issue Dec 4, 2016 · 14 comments

Comments

@cl0ne
Copy link

cl0ne commented Dec 4, 2016

Of course, I can override css_url with hardcoded URL. But how to use static tag to get css file url without overriding whole bootstrap3.html template?

@ikcam
Copy link

ikcam commented Dec 6, 2016

You can override the css_url on the settings.py file of your project without any hardcode.

@jieter
Copy link
Member

jieter commented Dec 6, 2016

@ikcam I think @cl0ne tries to ask how to get an url to the location of a static file in the css_url, which could be hard-coded.

You might succeed in doing it automatically, but you'll need some lazy approach: generating the url will require some settings to be defined so it cannot be returned as a string from a function ran in settings.py if that makes sense...

@cl0ne
Copy link
Author

cl0ne commented Dec 6, 2016

@ikcam It's not possible to use static within settings.py. Don't see any way to get equivalent to

<link rel="stylesheet" type="text/css" href="{% static 'bootstrap/dist/css/bootstrap.min.css' %}" />

@cl0ne
Copy link
Author

cl0ne commented Dec 6, 2016

@jieter You're right. Probably some wrapper with __str__ method will be sufficient, going to try this approach.

@cl0ne
Copy link
Author

cl0ne commented Dec 6, 2016

Thanks @jieter for idea! Came up with the following approach:

from django.templatetags.static import static

class StaticWrapper:
    def __init__(self, path):
        self.path = path

    def __str__(self):
        return static(self.path)

BOOTSTRAP3 = {
    'css_url': StaticWrapper('bootstrap/dist/css/bootstrap.min.css'),
    'jquery_url': StaticWrapper('jquery/dist/jquery.min.js'),
    'javascript_url': StaticWrapper('bootstrap/dist/js/bootstrap.min.js'),
}

@cl0ne cl0ne closed this as completed Dec 6, 2016
@dyve
Copy link
Member

dyve commented Dec 6, 2016

Good solution @jieter. But this could use a better fix, I'll look at examples form Django static urls to see if we could just apply static to the url.

@dyve dyve reopened this Dec 6, 2016
@gaelanadams
Copy link

@cl0ne did you define your class in your settings.py or another file?

@dyve Sorry I'm new to django would using staticfile prefixes work?

@cl0ne
Copy link
Author

cl0ne commented Jan 4, 2017

@gaelanadams I define it in separate file and then import in settings.py (even though it's only used there, but don't think it's good idea to not put it in separate file).
Sorry for late reply :)

@metajiji
Copy link

metajiji commented Jan 8, 2017

May be simply:

BOOTSTRAP3 = {
    'base_url': '%sbootstrap/' % STATIC_URL,
...
}

@dyve
Copy link
Member

dyve commented Jan 8, 2017

The most elegant solution would be if I ran the resulting url through static.

from django.contrib.staticfiles.templatetags.staticfiles import static
# Assume static path = '/static/'

url = static('bootstrap.css')
# url is now '/static/bootstrap.css'

url = static('https://example.com/bootstrap.css')
# url is now 'https://example.com/bootstrap.css'

url = static('/bootstrap.css')
# url is now '/bootstrap.css' -- so mind the starting slash

@melvyn-sopacua
Copy link

melvyn-sopacua commented Jan 23, 2017

I don't see the upside to loading a template tag for this.
What use case is there, where this does not resolve to STATIC_URL + path?
And STATIC_URL should be right there in settings.py.

So for example I'm using:

STATIC_URL = '/static/'
# ...
BOOTSTRAP3 = {
    'jquery_url': STATIC_URL + 'js/jquery.min.js',
    'css_url': STATIC_URL + 'css/bootstrap.min.css',
    'theme_url': STATIC_URL + 'css/bootstrap-theme.min.css',
}

This is analog to using {% static "js/jquery.min.js" %}. Only reason templates require a tag, is because they don't have access to settings.

@dyve
Copy link
Member

dyve commented Jan 24, 2017

The original reason for a template tag was to make it easy to use the same settings (JS, CSS, theme) in different templates. I agree that it's not a super big advantage, but it makes getting started easier.

@melvyn-sopacua
Copy link

It's a documentation issue. People perceive the static template tag as magical, yet all it does is put STATIC_URL before the argument.
This:

BOOTSTRAP3 = {
    'css_url': StaticWrapper('bootstrap/dist/css/bootstrap.min.css'),
    'jquery_url': StaticWrapper('jquery/dist/jquery.min.js'),
    'javascript_url': StaticWrapper('bootstrap/dist/js/bootstrap.min.js'),
}

and this:

BOOTSTRAP3 = {
    'jquery_url': STATIC_URL + 'js/jquery.min.js',
    'css_url': STATIC_URL + 'css/bootstrap.min.css',
    'javascript_url': STATIC_URL + 'js/bootstrap.min.js',
}

is no different to the end user, except that the first creates a circular dependency in the boot cycle of Django and loads a bunch of code. Solve this in the documentation, not the code.

Now, if you want to make things easy on end users, then support something like:

BOOTSTRAP3 = {
    'css_url': 'css/bootstrap.min.css',
    # ....
    'urls_static': True,
}

This avoids the circular dependency as you'd only have to transform them once one of the template tags is called and can simply defer to the static tag.

@cl0ne
Copy link
Author

cl0ne commented Feb 12, 2017

People perceive the static template tag as magical, yet all it does is put STATIC_URL before the argument.

The builtin template tag static which takes a path and urljoins it with the static prefix STATIC_URL.

If django.contrib.staticfiles is installed, the tag uses the url() method of the STATICFILES_STORAGE instead.

@dyve dyve closed this as completed Aug 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants