Is there a way to configure jekyll so that when it builds the static files in the _site folder it adjusts all the assets/urls so it works in a subfolder?
My setup is like this:
www.example.com/
I pushed all the _site files/folders to a subdirectory:
www.example.com/blog/
So now when I view my site, all the css/images are not rendering as they are all pointing to :
<link rel="stylesheet" href="/assets/vendor/bootstrap/css/bootstrap.min.css">
But the correct URL should be:
/blog/assets/vendor/bootstrap/css/bootstrap.min.css
I am using nginx for this, and I created a location section like:
location ^~ /blog/ {
alias /home/deploy/apps/_site/;
}
Have you tried using the baseurl setting in Jekyll? When hosting a project on GH it is basically serving out of a sub folder like you are trying to do, baseurl is the common way to deal with it.
In your config file set the baseurl:
baseurl: /blog
then update your templates to use Jekyll's relative_url filter to pull in the baseurl setting properly. For example:
<link rel="stylesheet" href="{{ 'assets/css/site.css' | relative_url }}">
or elsewhere
{{ page.url | relative_url }}
A less robust solution is to prepend all the urls with {{ site.baseurl }} directly like:
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/site.css">
There are various ways to build the url with the baseurl - these are just a couple of them.
If your site is already done and none of your links have baseurl built into them, there is no easy way to add it in other than going and updating all the links.
That said, there is not a lot of magic to baseurl - other than making it one place to edit it if it ever needed to be changed. That and there is something about jekyll serve that helps the site work in the browser better when doing local dev work (serve and build treat baseurl slightly differently I believe).
I've added
"twbs/bootstrap" : "3.3.5",
"components/jquery" : "2.1.4"
to my composer.json file.
I've run composer:update with no problems.
However now it's time to use assetic to create the files and composer used symlinks to install the components and twbs folders.
In the ::base.html.twig I would write something like the following...
{% block stylesheets %}
{% stylesheets filter = 'cssrewrite'
'%kernel.root_dir%/../vendor/twbs/bootstrap/dist/css/boostrap.css'
'%kernel.root_dir%/../vendor/twbs/bootstrap/dist/css/bootsrtap-theme.css'
'#AppBundle/Resources/public/css/custom.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
however the path for the rewrite is not correct because of the symlink installed by composer...
My question: Where did the files actually go?
Or is there another syntax to get at the files in twig?
I guess I need this... '%kerner.root_dir%/path/to/the/files'
every thing that you install through composer json goes to vendor folder (outside web folder)
you should check under web/bundles/ folder if you are in a linux or osx OS, if you are under windows, i don't think you'll have symlinks (so probably it's a hard copy each time you make a composer update). anyway. i think you should never use %kernel.root_dir% on twig.
in my case one of the bundles i'm using are symlinked under web/bundles to the real folder. as example guzzle :
lrwxrwxrwx 1 odin odin 88 ago 9 13:47 guzzle -> ../../vendor/eightpoints/guzzle-bundle/EightPoints/Bundle/GuzzleBundle/Resources/public/
so probably the path you should be using is:
bundles/twbs/bootstrap/css/boostrap.css
bundles/twbs/bootstrap/css/boostrap-theme.css
bundles/yourbundle/css/custom.css
and instead of using the public folder of app, at least in the documentation recomends adding css, js, and img folder inside web (if you are using them in more than one bundle)
I think you just need to do a php app/console assetic:dump in the terminal to dump the required assets into your web folder where they'll be publicly accessible to reference in your code.
I've put a css file in MyDirectoryBundle/Resources/public/css.
I then linked to it using:
<link href="{{ asset('css/main.css') }}" type="text/css" rel="stylesheet" />
I checked if it worked
short test: body {color: #FF0000;}
Nothing changed.
The parent layout (layout.twig.html) is in app/Resources/views (3 level architecture).
I still don't understand why:
css does not work
why the command: php console assets:install web/ --symlink fails
-> answer: The target directory web does not exist. (it does exist, actually).
I am quite new to symfony2 (2 weeks practice only).
I am working on Windows - just in case this may explain something...
Thank you very much for your help!
If the file first lived in MyDirectoryBundle/Resources/public/css, it'll be copied to web/bundles/mydirectory/css. So you have to include it like: {{ asset('bundles/mydirectory/css/main.css') }}.
You should execute the command from the root of your project. As you're using php console, it seems like you're in the app/ directory when executing this command. In that case, app/web/ does not exists, so the error is completely valid.
As you're on Windows, there is a change that symlink is not allowed/available. I believe it's available for admins only since Windows vista.
As a side tip, if MyDirectoryBundle is your app bundle (a bundle tied to your app and not meant to be reused by other apps), I would recommend to put the CSS file in the web/ directory directly. There is no need to put it in the bundle in such a case (this is only related to bundles that are shared, so the CSS is shared as well).
use php app/console instead of php console
I have a Jekyll site with some posts and some pages in the root directory.
If I navigate to a page in the root directory, such as localhost:4000/index.html all of the files in the _includes directory load no problem using:
{% include head.html %}
If I then go to a post using the permalink format as defined in config.yml:
permalink: /:categories/:title
localhost:4000/blogpost/first-post
The include files are not loaded. Looking in Firebug at the CSS files in the header it gives an error that the file is not found and is looking in the directory:
/blogpost/first-post/css/boostrap.min.css
If I give the post a permalink in YAML as:
permalink: /first-post.html
Everything works fine.
How do I set up the include to find the right files in my pages when using permalinks to navigate?
Includes and assets are two different things.
Includes are partials that are usually stored in _includes. If include anyfile.html work in index.html, it will work in any other page or post.
assets like js, css or images are loaded by html following a path. It's better to use a path relative to the site root. That's why Jekyll calls assets like this :
<link rel="stylesheet" href="{{ "/css/main.css" | prepend: site.baseurl }}">
So, if your site is reached at http://localhost/any/path, your _config.yml look like this :
url: http://localhost
baseurl: /any/path
And then, no more problems with assets !
I made a Django admin site using Django development version but it isn't being styled:
After setting up your STATIC_ROOT and STATIC_URL, you may have to run
python manage.py collectstatic
ADMIN_MEDIA_PREFIX is deprecated now, use STATIC_URL instead. Setting STATIC_URL = '/static/' in settings.py should do the job. Try:
import os.path
import sys
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
and then:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
Works on Django 1.4 pre-alpha SVN-16920.
I broke my head over it for two days trying whatnot!
Finally, changed DEBUG in the settings.py file to:
DEBUG = True
and it worked.
P.S:
SECURITY WARNING: don't run with debug turned on in production!
Django does not serve static files on it's own. You have to tell it where the files are.
The ADMIN_MEDIA_PREFIX in the settings.py will point Django in the right location.
Since you're using the development version, you'll want the dev-specific document for static files how-to. Adam's link will lead you to the 1.2 version.
I read several other threads trying to fix this...resorted to an alias as in other threads.
This assumes that your own custom app is serving static files correctly, which would indicate that your STATIC_ROOT and STATIC_URL have proper settings.
STATIC_ROOT = ''
STATIC_URL = '/static/'
Then (from your static directory):
ubuntu#ip-1-2-3-4:/srv/www/mysite.com/app_folder/static$ sudo ln -s /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/ admin
Hope this helps someone...there are a lot of threads on this topic.
I ran into this issue as well following the Django Book Tutorial.
In Chapter 5|Installing the model, the book states when referring to the default INSTALLED_APPS-
"Temporarily comment out all six of those strings by putting a hash character (#) in front of them."
http://www.djangobook.com/en/2.0/chapter05.html
Then, in Chapter 6, the Book tells the reader to uncomment 4 of those 6 lines-
"note that we commented out these four INSTALLED_APPS entries in Chapter 5. Uncomment them now."
But the statcifiles line is what is needed to restore CSS to the admin page, so uncomment that
'django.contrib.staticfiles',
In /project_name/project_name/settings.py you need to set STATIC_URL to tell your site what url to use for static files.
Then set STATIC_ROOT to be some folder on your filesystem that is not the same as any of your directories listed in STATICFILES_DIRS list.
Once STATICFILES_ROOT is set, you would run python manage.py collectstatic from the project directory.
This will copy all the admin static files and all files in any other folders listed in the STATICFILES_DIRS list. Basically this puts all your static files in one place so you you can move them to your CDN when deploying your site. If you are like me and don't have a CDN, then you have two options:
Add the folder you set as STATIC_ROOT to the STATICFILES_DIRS list. This will allow the staticfiles finders in django to locate all the static files.
Move the entire folder of static files somewhere else on your file system and direct STATICFILES_DIRS to include that new location.
I make no comments about security with this answer, it is just the way I have been able to develop with my web server for small projects. I expect that you will want a CDN as django suggest if you are doing anything larger scale.
UPDATE:
I just ran into this issue and this method didn't quite do what I think you want. What ended up working for me was after I ran collectstatic I just copied the admin static files that it put into STATICFILES_ROOT into the directory that I had used for my own static files. That solved the issue for me.
In addition to many of the other answers being useful, I had a problem that hasn't yet been noted. After upgrading from Django 1.3 to 1.6, my static files directory had a broken symbolic link to the django admin static files.
My settings.py was configured with:
STATICFILES_DIRS = (
'/var/www/static/my-dev',
)
According to this answer,
Django will now expect to find the admin static files under the URL
/admin/.
I had a symbolic link /var/www/static/my-dev/admin which was set to:
admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/
That location no longer exists in django 1.6, so I updated the link to:
admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/
And now my admin site is working properly.
run: python manage.py collectstatic
Add this line to Vhost which located at : /etc/apache2/sites-available/000-default.conf
Alias /static/admin/ /var/www/html/example.com/static/admin
Here is entire Vhost setting for django setup
<VirtualHost *:80>
ServerName gautam.tech
ServerAlias www.gautam.tech
WSGIDaemonProcess gautam.tech python-path=/var/www/html/gautam.tech python-home=/var/www/html/gautam.tech/venv
WSGIProcessGroup gautam.tech
#Your static files location
Alias /static /var/www/html/gautam.tech/static
Alias /media/ /var/www/html/gautam.tech/media
Alias /static/admin/ /var/www/html/gautam.tech/static/admin
<Directory /var/www/html/gautam.tech/static>
Require all granted
</Directory>
<Directory /var/www/html/gautam.tech/media>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/html/gautam.tech/myproject/wsgi.py
DocumentRoot /var/www/html/gautam.tech
<Directory /var/www/html/gautam.tech>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
CustomLog /var/www/html/gautam.tech/access.log combined
ErrorLog /var/www/html/gautam.tech/error.log
</VirtualHost>
This will work for sure!
I see there are many answers but none of them worked for me, so I'm posting my own.
What solved it for me was adding a static files URL to the root URLs of the app. I needed to add this URL to my URLs list:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
You will also need these two imports:
from django.conf import settings
from django.conf.urls.static import stati
More can be viewed in this article.
If you are using Apache server to host your django site, you need to make sure the static alias point to your /directory to site/site_media/static/. If your static files are in /directory to site/site/site_media/static/, the previous Apache alias configuration will not work.
While following the Django tutorial, I had a similar problem and in my case the issue was the mimetype used by the development server when serving css files.
The mimetype served was 'application/x-css' which led to following warning message in Chrome (in the 'Network' tab of the Developer tools):
Resource interpreted as Stylesheet but transferred with MIME type
application/x-css: "http://127.0.0.1:8000/static/admin/css/base.css"
The workaround that I've found: changing the mimetype to be served by adding following lines to the django webapp's manage.py file:
import mimetypes
mimetypes.init()
mimetypes.types_map['.css'] = 'text/css'
Note: worked for me with Django 1.7.4 on Python 2.7 and Chrome 40.0
Same sort of issue i encountered while developing a site in django-1.10.5 and python-2.7.13. But in my firefox-51 and chrome, the login page was able to get the css but still there was no styling. But weirdly it was working on IE-8..
I tried do every possible thing mentioned here and suitable to my set of sw versions. None worked.
But when i tried the same site on other system which had the python-2.7.8, it worked..
Just posted if it may help someone...
edited: later I found that in python-2.7.13, writing the following two lines in settings.py (plus clearing the cache of the browser) had done the trick
import mimetypes
mimetypes.add_type("text/css", ".css", True)
My issue was resolved by creating new Virtual Environment for the project, before that I was using general system level python interpreter.
$ mkvirtualenv myproject
Reference: https://docs.djangoproject.com/en/2.1/howto/windows/
In the issue is in a dev/test/prod server and using Nginx, please follow the below steps.
set the configs in settings.py as something below
STATIC_URL = '/static/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Run the below command to create css and js files in static folder
$ python manage.py collectstatic
config in /etc/nginx/sites-enabled/example (Nginx) to serve static files
location /static/ {
alias /project/root/folder/static/;
}
this works fine and easily. I moved (manually) the folder. just you have to copy your static/admin from the directory of the main Project and paste it into public_html static/ if there is no static folder you have to run following command in terminal
python manage.py collectstatic
here you go with css working of Django admin
Ensure that 'django.contrib.staticfiles' is in your INSTALLED_APPS in your settings.py
Admin panel was working fine except css wasn't loaded. This worked for Lightsail Django with Apache
1.Define STATIC_ROOT and STATIC_URL in settings.py
STATIC_ROOT = '/opt/bitnami/projects/decisions/decision/'
STATIC_URL = '/static/'
2.Eject(copy) admin assets files to the project
run python manage.py collectstatic
this command creates /opt/bitnami/projects/decisions/decision/admin folder with css/ fonts/ img/ js/ subfolders
3.Make /static url accessible from apache
Paste this snippet in /opt/bitnami/apache2/conf/bitnami/bitnami.conf (If you have set up ssl then the file location will be /opt/bitnami/apache2/conf/bitnami/bitnami-ssl.conf)
Alias /static/ "/opt/bitnami/projects/decisions/decision/"
<Directory "/opt/bitnami/projects/decisions/decision/">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
4. Don't forget to restart apache
sudo /opt/bitnami/ctlscript.sh restart apache
Failing after trying 1000s of suggestions, I finally found a solution that helped. Here is what I tried and what I was using.
I am using django-1.11 and nginx web server.
Firstly, I made sure that my CSS/js files are not getting 404 in browser's console. After that, I could see a warning
Resource interpreted as Stylesheet but transferred with mime type text/plain
I found the base.html in admin templates and removed
type="text/css"
and now the lines looks like this:
<link rel="stylesheet" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}" />
This fixed the issue for me.
Check your settings.py file
STATIC_URL = '/static/'
there should be backslash ' / ' in both opening and closing side ..
If you have a value set in settings.py for STATICFILES_DIRS and the declared folder doesn't exist or is in the wrong location, it will cause the Admin to have no styling e.g. by defining:
STATICFILES_DIRS = ( os.path.join(BASE_DIR,"static"))
And the static folder doesn't exist .
Configuring static files
Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
In your settings.py file, define STATIC_URL, for example:
STATIC_URL = '/static/'
For more details see
static filesĀ [django-docs]