Django_compressor hide the css? - css

I am using django_compressor.
In template :
{% load compress %}
{% compress css %}
<link rel="stylesheet" href="/media/css/master.css" type="text/css" charset="utf-8">
{% endcompress %}
In settings :
DEBUG = True
MEDIA_URL = '/media/'
INSTALLED_APPS = [
'compressor',
// Here other installed app.
]
But When I do these things my css is not getting uploaded. When I remove the {% compress css %} tag My css start rendreing. Where I am doing mess? Can any one suggest me?

I would check two things:
if compress has access to the file itself
is generated file (e.g. under /media/cache/....) is accessible by browser

In settings COMPRESS is a boolean that decides if compression will happen. Default value is the opposite of DEBUG. So you should change DEBUG = True to DEBUG = False.
(https://github.com/carljm/django_compressor)

Related

django cms 'TemplateResponse' object has no attribute '_headers'

I am following the django-cms documentation and so far I only have one template home.html:
{% load cms_tags sekizai_tags %}
<html>
<head>
<title>{% page_attribute "page_title" %}</title>
{% render_block "css" %}
</head>
<body>
{% cms_toolbar %}
{% placeholder "content" %}
{% render_block "js" %}
</body>
</html>
And my url.py looks like this:
from django.contrib import admin
from django.urls import path, re_path, include
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include('cms.urls')),
]
I can login and create a page, but when I logout and navigate to `localhost:8000' I get:
AttributeError at /
'TemplateResponse' object has no attribute '_headers'
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 3.2
Exception Type: AttributeError
Exception Value:
'TemplateResponse' object has no attribute '_headers'
Exception Location: /usr/local/lib/python3.9/site-packages/cms/cache/page.py, line 84, in set_page_cache
Python Executable: /usr/local/bin/python
Python Version: 3.9.4
Python Path:
['/app/app',
'/usr/local/lib/python39.zip',
'/usr/local/lib/python3.9',
'/usr/local/lib/python3.9/lib-dynload',
'/usr/local/lib/python3.9/site-packages']
I am not sure if I did something wrong here. I would like to see how the webpage looks like for someone who does not have an account and cannot login.
Support for django 3.2 has been introduced in django-cms 3.9 so upgrade to this version to resolve this error.

How to set up SASS to work with Django and AWS S3

I'm trying to set up my Django project to use SASS, unsuccessfully so far.
I've installed libsass, django-compressor, and django-sass-processor.
My settings.py includes these lines:
INSTALLED_APPS = [
'sass_processor',
.... [my other apps here]
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'sass_processor.finders.CssFinder',
]
SASS_PROCESSOR_ROOT = os.path.join(PROJECT_ROOT, 'static/css')
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_STORAGE_BUCKET_NAME = 'myproj-debug' if IS_DEBUG else 'myproj'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_DEFAULT_ACL = 'public-read'
AWS_LOCATION = 'static'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'myproj.storage_backends.MediaStorage'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
My base.html includes these lines:
{% load sass_tags %}
<link href="{% sass_src 'app.scss' %}" rel="stylesheet" type="text/css">
When I run my debug Django server with ./manage.py run server, the processor seems to find my app.scss file okay, because when I move it to a different location I get an error. But where is the compiled app.css file being placed?
I see in my page source that the {% sass_src line becomes <link href="https://myproj-debug.s3.amazonaws.com/static/app.css" rel="stylesheet" type="text/css"> but no file shows up there. (I'd actually like to have it placed at https://myproj-debug.s3.amazonaws.com/static/css/app.css but that's another matter.)
How do I get app.css to compile and show up in my AWS S3 bucket like it should?

Django static files link

thanks for looking into this, cause it's getting on my nerves now:)
I can't get a style.css (or any static file to get oared at all!) and I have googled around and none of the solutions worked so far.
my urls.py:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
urlpatterns = patterns('',
url(r'^$', 'ports.views.home', name='home'),
)
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^static/(?P<path>.*)$', 'serve'),
)
my base.html where i load the statics:
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="static/css/style.css" type="text/css" />
...
my settings.py references the static directory:
STATIC_URL = '/static/'
I don't understand what I am doing wrong (or in fact why it has to be so awkward to server these statics). any help in lakers terms would be appreciated!
Thanks,
blargie-bla
On your server, you probably need to try this
python manage.py collectstatic -l
The -l makes a symbolic link instead of copying. This prevents two copies of the same file and save some space, but also means if you rename the original file, you'll have to reestablish the link.
More info about the collectstatic command here.
Don't forget to set STATIC_ROOT as well.
Just for reference here is my settings.py for that part:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(os.path.dirname(__file__), '..', 'static').replace('\\', '/')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
This applies for Django 1.4+, where the settings.py is under (for example) mysite/mysite/ and it's not on the same level as the static folder.
My problem was adding the css outside block content. Moving it inside the block content solved the issue.
{% extends "home/base.html" %}
{% load static %}
<!--<link rel="stylesheet" href="{% static 'blog/main.css' %}">-->
{% block content %}
<link rel="stylesheet" href="{% static 'blog/main.css' %}">
--some code--
{% endblock content%
Try:
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css" />

Django: Is it possible to attach media files (css, javascript etc) to a View-class?

I can't fins any information on how to define css or javascript files in a view like:
class MyView(View):
....
class Media:
css = { 'all' : 'mystyle.css' }
If you have a form you can do like:
class MyForm(ModelForm):
....
class Media:
css = { 'all' : 'mystyle.css' }
And then in the template you can print the files like;
{{ form.media.css }}
I like that Syntax very much and I like to keep the View-specific css files in the app-directory.
Does anyone know if it's possible?
Thanks!
I imagine you should be able to set a variable with the css filename/location in your view, and if you pass that variable to the render/response you should be able to access it in your template.
Take a look at https://docs.djangoproject.com/en/dev/ref/templates/api/#variables-and-lookups
example:
def my_view(request):
stylesheet = {"css": "my_view.css"}
return render(request, 'myapp/templatename.html', stylesheet,
content_type="application/xhtml+xml")
and them templatename.html would be
<link rel="stylesheet" type="text/css"
href="{{ MEDIA_URL }}{{ stylesheet["css"] }}" media="screen"/>

How to combine these assetics in Symfony2?

I'm using ExposeTranslationBundle (expose translations to javascript) and JMSI18nRoutingBundle (expose routes to javascript). This is part of my <head> tag:
{% javascripts filter='?yui_js' output='js/app.js'
'../app/Resources/public/js/jquery-*.js'
'../app/Resources/public/js/jquery/*'
'../app/Resources/public/js/app.js'
'bundles/fosjsrouting/js/router.js'
'bundles/bazingaexposetranslation/js/translation.js' %}
<script src="{{ asset_url }}" ></script>
{% endjavascripts %}
<!-- ExposeTranslationBundle and JMSI18nRoutingBundle -->
<script src="{{ path('fos_js_routing_js',
{"callback": "fos.Router.setData"}) }}"></script>
<script src="{{ url('bazinga_exposetranslation_js') }}"></script>
Is possible to combine the last two <script> imports into first assetic and how?
I thing it is not possible because the FOSJSRouting javascript file is generated by a controller. Internaly the bundles caches the js but in app/cache, so it needs to go through the controller every request. I'm not familiar with the expose translation bundle but I guess it's the same problem here.
There has been an ongoing discussion in the issue tracke of FOSJsRouterBundle on github and there is also a sollution. See the full issue here: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/issues/22
The workaround is to have a script or command to dump the output to files in web/js directory:
<?php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('stage', false);
$kernel->loadClassCache();
$response = $kernel->handle(Request::create('/js/routing?callback=fos.Router.setData'));
file_put_contents(__DIR__.'/../web/js/routes.js', $response->getContent());
This is somewhat of a workaround sollution. I've been thinking about implementing a generic bundle wich whome this task can be configured for several other bundles using controllers to output js. The controller actions would have to be configured in a yml file and then a command would have have to be executed at each deployment/modification of routes/strings. But I havn't had time for this... yet ;)
Instead of import, you could happily put it inline, ie:
<script type="text/javascript">
{# BazingaExposeTranslation #}
{% render 'bazinga.exposetranslation.controller:exposeTranslationAction'
with { domain_name: "messages", _locale:app.session.locale, _format: "js" } %}
{# JMSI18nRoutingBundle ... #}
</script>
You need to check the routing file for those bundles.

Resources