I've got Wagtail set up with Django-Compressor (COMPRESS OFFLINE = True) which all works except for one template.
This template uses the Disqus plugin which requires:
An absolute page url
A unique page identifier
In my template I have included the following:
{% compress js inline %}
// My JS Code
this.page.url = "{{ page.full_url }}";
this.page.identifier = {{ page.id }};
{% endcompress %
So when accessing the page I get the error You have offline compression enabled but key "ARANDOMKEYGOESHERE" is missing from offline manifest. As per the Django-Compressor docs:
If you use any variables inside the {% compress %} blocks, make sure to list all values you require in COMPRESS_OFFLINE_CONTEXT
How would I go about adding the Page instance to the context in my settings? I thought of something like (pseudo-code):
COMPRESS_OFFLINE_CONTEXT = { 'page': request.get('page') }
Related
I have a template which I am going to send as an email.
Thus I need to have absolute full urls (along with protocol and domain name) instead of relative ones.
The content in the email is going to come dynamically from database (entered using ckeditor, so I CAN NOT do something like {{ protocol }}{{ domain_name }}{% static '' %}. This would work only for static files. However the media content uploaded via ckeditor will recide in media files and I have absolutely no control over it.
Also i cant use javascript as it is an email template.
Currently I have built a python function which scans the entire template after rendering and prepends the protocol and domain name to every src attribute in img tag and all href attributes.
I would like to know if any better way exists
You can use request.build_absolute_uri and make a custom template tag in order to used when rendering your mail template.
Example
#templatetags/url_helper.py
#register.simple_tag()
def full_uri(request, relative_url):
return request.build_absolute_uri(realtive_url)
Then ...
{# Some template.html #}
{% full_uri request some_img.url as full_img_url %}
<img src={{ full_img_url }} />
I am building an application with Django as the backend and React js for making the interface of the application.
I have a set of Posts which I want to display.
Currently, the approach which I am following is -
Get the template having the compiled js code linked to it.
Then again make get call to get the posts
My question is - In this current approach I am making 2 GET calls to the backend, one for rendering the template and then again for getting the Post.
What is the best way to achieve this? Is this the usual flow how applications are built using Django and React JS?
First off: I don't see anything wrong with doing this in two requests, because one loads the application itself and the second loads the content. To me this seperation makes sense and might turn out to be useful in the future if you want to reuse say the Endpoint, that yields the the posts(i.e. the content).
Answering your question: If, for whatever reason, you absolutely want to load everything with a single GET, a good way of doing so, would be to pass a list of posts to the context as a JSON-serialized object and then load these into the JS-context within the Django-template.
i.e. in the view.py:
from json import dumps
def view(request):
context = {
'posts':get_posts(),
}
render_to_response('django_template.html', context,
context_instance=RequestContext(request))
def get_posts():
qs = Posts.objects.all()
return dumps({'posts': qs })
in the django_template:
{% block content %}
<div id="root"></div>
{% endblock %}
{% block javascript %}
<script>
var INITIAL_POSTS = {{ posts|safe }};
</script>
<script type="text/javascript"
src="PATH_TO_STATIC_REACT_ASSET.JS"></script>
{% endblock %}
you should now have your posts in your JS context and can load them in your React component. Once again: I would agree with Daniel Rosemans comment
I am working on an informational site. I am attempting to access files in a subdirectory using data from the django db.
Ex: if the record.id = 1 and the file is desc.txt, I want to grab /1/desc.txt
I currently have two items that I'm trying to access this way. The image file will work if I hard code the location, but I haven't been able to get it to work from the record data. The other one, I'm trying to load a description file into a div using jQuery and .html(), and can't get it to work at all.
The relevant section of code:
{% if aRecord %}
{% load static from staticfiles %}
<h1>{{ aRecord.Name }}</h1>
<br/>
Born: {{aRecord.Birth}}<br/>
Died: {{aRecord.Death}}<br/>
<div id="descArea"><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></div>
<script>
descfile = 'Authors/1/desc.txt';
$('#descArea').text(descfile);
$.get("{% static 'Authors/1/desc.txt' %}", function(data) {
$('#descArea').html(data);
});
</script>
<br/>
{% else %}
Requested author not found.
{% endif %}
This is the hard-coded version that works, but I need it to work from the aRecord data. The 1 in both cases should come from the record id, and the image filename in the img tag should as well.
It sounds like this is really a question about how to produce a file path from dynamic data. Is that the case? If so, you will probably need to use string substitution.
In order to produce a string like '/1/desc.txt' where the 1 is given by a record id, you could do something like this:
path = '/{}/desc.txt'.format(record.id).
I've got a django-cms site on which I have created a page at /managers-home/ with an app hook so that I can use myapp from that page.
myapp renders various templates at various URLs beneath /managers-home/ and I would like each of these templates to have a section editable via the django-cms content plugin. Therefore I have added {% staticplaceholder "content" site %} to these templates, because, as I understand it, you can't use a standard {% placeholder "" %} from within a hooked application.
I made a start with this and added some text to the placeholder on /managers-home/page-1/ which uses page-1.html and then when I got to the placeholder on /managers-home/page-2 I could already see the content from page-1 despite now using page-2.html so the placeholder on these two individual templates is being shared.
How can I correctly add django-cms placeholders throughout my application templates?
Turns out my problem was that a static_placeholder is exactly that, just a placeholder identified by the name given and anywhere you reference that name you get the same content.
So in order to allow each of my templates to display custom text, I've created a static_placeholder for each template.
# page-1.html
{% static_placeholder "page-1" site or %}
Default text goes here
{% endstatic_placeholder %}
# settings.py
CMS_PLACEHOLDER_CONF = {
'page-1': {
'plugins': ['TextPlugin', 'UploadedPicturePlugin'],
'text_only_plugins': ['LinkPlugin'],
'extra_context': {"width": 640},
'name': gettext("Content"),
}
}
I'm using Flask to build a website.
I also use assets to manage my js and css resources.
Now I want to pass some variables to my js scripts.
here is the code I register assets when the flask init(__init__.py):
download_js = Bundle("applog/download.coffee", filters=["jinja2", "coffeescript"],
output="gen/js/download.js")
assets.register("download_js", download_js)
here is the code I use the asset in my template file(download.html):
{% assets "download_js" %}
<script>
require(["{{ ASSET_URL}}"]);
</script>
{% endassets %}
and in view file("view.py"), I pass the varibales like this:
return render_template("download.html", apps=apps, versions=versions)
and I want to use the variable "apps" in the js file like this:
DownLoadSelector("appList", {{ apps }})
How can I do that?
Your assets are static, you can't change it in an easy way. You can add a filter to webassets, but it is a weird hack.
To do what you want, you can do it:
{% assets "download_js" %}
<script>
window.apps = {{ apps }};
require(["{{ ASSET_URL}}"]);
</script>
{% endassets %}
In your code, you just call apps:
DownLoadSelector("appList", window.apps)