How to locate websocketbridge.js in Django using channels websocket? - django

I am trying to implement websockets using channels in Django project. I am getting 404 for webscoketbridge.js Below is html template.
{% load staticfiles %}
{% block title %}Delivery{% endblock %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="{% static 'channels/js/websocketbridge.js' %}" type="text/javascript"></script>
Also, I tried to have a look in the virtualenv/lib/python3.5/site-packages/channels path, there is no js folder or any file named websocketbridge.js
Has anyone solved this issue?

The javascript bridge was removed in v2.1.4. Here's the commit: https://github.com/django/channels/commit/2a9d764ad03927581aa2bfcadccc3e953949cb98#diff-b582cbb2f8294afa8bbe26c4c360a01d
This bit me, in my book that breaks semantic versioning.

As #tobyspark said, the javascript wrapper has been completely removed in the Django-channels 2. You can read more on how the js WebSocket wrapper was working in channels 1 here.
the simplest workaround to clear that error in your browser is to create a file called websocketbridge.js in the path shown in the error, "static/channels/js/", or you can specify any other path in your HTML src attribute matching the location of the static files and then add the code from here.
But you have to find a better implementation. You can use ReconnectingWebSocket. In the channels 2 release documentation, it is stated there might be other third-party packages for the binding but I don't know any other.

Related

Using Foundation Modals with Django

I had an html file using Foundation to display a simple signup login template that was working as desired before using Django. I made a Django project and app and was able to get everything displaying as it was before except for modals. The section looks like this:
<a data-open="signup" class="button">Sign up</a>
<div class="reveal" id="signup" data-reveal>
I've used this http://foundation.zurb.com/sites/docs/reveal.html but it's not actually displaying the modal when I run the Django server.
I also have the following lines in my html page:
<link rel="stylesheet" href="{% static "MyApp/foundation.css"%}">
<link rel="script" href="{%static"MyApp/js/jquery/vendor/foundation.js"%}">
Along with this <script> $(document).foundation(); </script>
These are all in my static directory. I also have tried adding 'foundation' to INSTALLED_APPS in my settings but I get an error saying no module was found. However, I'm using other CSS from foundation and it's working fine so I'm wondering if this issue has something to do with using foundation in my static directory vs. using this package: https://pypi.python.org/pypi/django-zurb-foundation
Any help is appreciated!

Serve different Static files on devel and production in Django

I have a production and local DJANGO development environment. To push things to production we have a deployer which minifies and gzips all CSS and JS files.
To serve them on production I need to call them like
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">
However on development I want the normal css file served (that way I don't have to re-minify and gzip each time I save) with:
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">
Is there any way to achieve and automatize this behaviour by adding something to the deployer?, is there some other work-around (I could get rid of the .min extension if it's possible to add the .gz in a clean way?
I want to note the I know I could implement some html-parser which adds it on each deploy but I'm looking for a neat and django oriented solution.
I like the #Nursultan idea. To enforce this you could code a context processor like this:
# On yourapp.context_processor.py
from django.conf import settings
def debug_set(request):
return {'debug_set': settings.DEBUG}
# On your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
.
.
.
'yourapp.context_processors.debug_set',
)
# On your templates
{% if debug_set %}
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">
{% else %}
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">
{% endif %}
As usual, there's a Django package for that! There are two I use:
django-compressor: http://django-compressor.readthedocs.org/en/latest/
django-pipeline: https://django-pipeline.readthedocs.org/en/latest/
I started on django-pipeline but have moved to using compressor as of late. See the docs, I believe one will be what you're looking for. Good luck!
I have never met this problem, but I come up with these two solutions:
Use different settings.py for production and development. But it requires to have the same names for *.min.js and change minifier's configuration.
Or use a global variable and write everywhere
{% if development_stage %}
<link>
{% else %}
<link>
{% endif %}
Django - How to make a variable available to all templates?

Go language strange behavior by handling templates

gotemplates
Hello!
I'm learning Go language now and trying to port some simple WEB code (Laravel 4).
Everything was well, until I tried to reproduce Blade templates into text templates.
I found that Go can load my CSS and JavaScript files only from the catalog with a name "bootstrap" only.
Here is my catalog tree which I tried to use:
start-catalog
bootstrap (link to bootstrap-3.3.1)
bootstrap-3.3.1
css
bootstrap.min.css
js
bootstrap.min.js
jquery
jquery (link to jquery-2.1.1.min.js)
jsquery-2.1.1.min.js
go_prg.go
Here are my templates:
base_js.tmpl
{{define "base_js"}}
{{template "login_1"}}
<script src = "/bootstrap/js/jquery"></script>
<script src = "/bootstrap/js/bootstrap.min.js"></script>
{{end}}
base_header.tmpl
{{define "base_header"}}
<head>
<title>PAGE TITLE</title>
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<link href = "/bootstrap/css/bootstrap.min.css" rel = "stylesheet">
</head>
{{end}}
If the catalog name differs from "bootstrap" Go language or Firefox can't load files from the templates above: bootstrap.min.css, bootstrap.min.js, jquery.
If I use not the link but the catalog name directly "bootstrap-3.3.1" than Go or Firefox can't load.
If all required files are moved under "bootstrap" I'm getting the results I expected (exactly the same as in Laravel 4).
To launch go language code the command go run go_prg.go was used.
Environment: Ubuntu 14.04, go-1.3.3, Firefox 31.
Who's wrong: Go language, Firefox or me?
Any help will be highly appreciated!
The problem described was caused by
http.Handle("/bootstrap/", http.StripPrefix("/bootstrap/", http.FileServer(http.Dir("bootstrap"))))
before any template was handled. It allowed access files under the directory 'bootstrap' only.
The problem was fixed by changing to
http.Handle( , http.StripPrefix(, http.FileServer(http.Dir("."))))
and adding to pathes for CSS and JavaScript files. Like so
/bootstrap/js/jquery">.

How to import dajaxice?

I'm a nooby to django and I tried many hours to get a simple example of dajaxice running, but I don't seem to find the right way to look for the files.
I did and redid installation and tried to find answers in the numerous similar questions on stackoverflow like this one and this one.
I put {% dajaxice_js_import %} in the header of myapp_index.html which prints out as:
<script src="/static/dajaxice/dajaxice.core.js"
type="text/javascript" charset="utf-8"></script>
but it cannot find this file:
ImproperlyConfigured: The storage backend of the staticfiles finder doesn't have a valid location.
And the get fails:
GET /static/dajaxice/dajaxice.core.js HTTP/1.1" 500 59
Strangely enough dajax loads:
<script type="text/javascript"
src="{% static /static/dajax/jquery.dajax.core.js" %}"></script>
Here's my folder structure:
myproject
----manage.py
----myproject
--------settings.py
--------urls.py
----myapp
--------ajax.py
--------urls.py
--------templates
------------myapp_index.html
I also haven't really understood why we need two urls.py files, but somehow it seems to access myapp_index.html if I put
from django.views.generic.simple import direct_to_template
and then
url(r'^$', direct_to_template, {'template': 'myapp_index.html'}),
in myapp's url patterns.
I also tried uncountable filenames in
python manage.py findstatic dajaxice.core.js
but somehow it doesn't find dajaxice, even though dajaxice is installed and accepted in the settings.py file among the INSTALLED_APPS.
Also python manage.py collectstatic fails for the same reason, but if I understood correctly, I don't event have to make it run as long as I'm on a development server.
I guess I have some basic misunderstanding of the underlying structure. :(
I'm using the prepacked latest ubuntu packages:
django: 1.4.5,
dajaxice: 0.5.5
Thanks in advance for any hint!
here is the template file:
{% load static %}
{% load dajaxice_templatetags %}
<html>
<head>
<title>My base template</title>
{% dajaxice_js_import %}
<script type="text/javascript" src="{% static "/static/dajax/jquery.dajax.core.js" %}"></script>
<script type="text/javascript">
function my_js_callback(data){
alert(data.message);
}
Dajax;
Dajaxice;
</script>
</head>
...
<button onclick="Dajaxice.myproject.myapp.sayhello(my_js_callback);">Click here!</button>
I get no Django error, the page shows, but I get this in Firebug:
"NetworkError: 500 Internal Server Error - http://localhost:8000/static/dajaxice/dajaxice.core.js"
and this:
ReferenceError: Dajaxice is not defined
Dajaxice;
It seems that you've messed up your urls.conf. It should contain something like:
url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),
Does it?
Also, the STATICFILES_FINDERS section of your settings.py file should include:
'dajaxice.finders.DajaxiceFinder',

Generate barcodes in Django site

I want to add barcode generation in a Django site and wonder what the best library or api would be. My first preference is something callable from Python - either written in Python or a C/C++ lib that I can wrap with ctypes/SWIG. Otherwise I can call out to the command line if must be.
I need at least EAN and UPC symbologies.
I've tried pybarcode but the image quality is too low. And Elaphe looks promising but from the Python interpreter all I could make was a QR Code -- EAN and UPC errored out (maybe because the syntax/usage was unclear from the documentation).
Use pybarcode and generate the barcode as SVG: http://packages.python.org/pyBarcode/barcode.html#creating-barcodes-as-svg
No problem of image quality in that case.
This thread is quite old, but in case anyone else is looking for an answer to this... code39 is a font, as are most types of barcode. You can simply use google fonts:
https://fonts.google.com/specimen/Libre+Barcode+39+Extended+Text?selection.family=Libre+Barcode+39+Extended+Text
Aside from that option, you could host static files, one solution could be this project on github:
https://github.com/Holger-Will/code-39-font
In that project all you need are the files associated with the size you want, and the code39_all.css file. The rest you could delete, if you like.
For your reference, I'm using both here:
{% load staticfiles %}
{% load static %}
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Libre+Barcode+39+Extended+Text" rel="stylesheet">
<link rel="stylesheet" href="{% static 'code-39-font-master/code39_all.css' %}"/>
</head>
<body>
<style>
body {
font-family: 'Libre Barcode 39 Extended Text', cursive;
font-size: 48px;
}
</style>
<div>I'm just a code39 google font</div>
<div><class="code_39_S">I'm generated with static files!</div>
</body>
</html>
reportlab could be a good alternative to pybarcode, especially when using some of its other features.
There is a howto for barcodes in Django with reportlab, works well for me.
https://code.djangoproject.com/wiki/Barcodes