In PyCharm: "Unused local variable..." using multiple JavaScript template files - python

I'm using PyCharm for my Django project and In my file product_header_js.html:
<script>
{% include 'global_variables.js' %}
{% include 'update_scene_function.js' %}
...
// I may use variables in here or in update_scene_function.js
</script>
I would like to use variables in global_variables.js in my update_scene_function.js file and product_header_js.html file. PyCharm lets me do this, but gives me the error Unused local variable... in global_variables.js and all my variables are greyed out because PyCharm doesn't think they are being used. Is there anyway to fix PyCharm?

So what generated this question was me trying to modularize my JavaScript code, but it looks like I might have to just deal with the PyCharm warnings. Even when I add JavaScript as a template file type, PyCharm's JavaScript interpreter still gives me warnings (not Django template tag warnings) when I do something like this:
{% if something %}
var my_var = "a";
{% else %}
var my_var = "b";
{% endif %}
It'll say Duplicate declaration. And when I reference a variable not in the current template file or when I don't use a variable declared in the current template, PyCharm complains.
So in summary, you can do this, but PyCharm will complain and the CTRL + clicking to jump to declarations doesn't work.
One of my follow-up questions would be, if you can tolerate the PyCharm warnings, is including JavaScript files better performing than multiple external JavaScript files (). I would think including would perform better most of the time (except if most of your traffic is returning visitors where you could use external JavaScript files and leverage caching).

Related

Generating audio files in a Django project template

I am attempting to create a pretty simple website using Django that will have a number of audio files stored in it and available for streaming and downloading. I know that there are other ways to do this, like using AWS, but I wanted to figure this way out first. I'll include my template here but wanted to note that it is currently messy because I have been experimenting with different structures to figure this problem out.
{% for song in concert.song_set.all %}
<li>{{ song.song_title }}</li>
<li>{{ song.song_location }}</li>
<!-- Working -->
<audio
class = "audioPlayer uniqueShowAP"
controls <--controlsList="nodownload"
src="{% static 'shows/audio/redRocks2019/01 Yi.mp3' %}">
Your browser does not support the
<code>audio</code> element.
</audio>
<!-- Not Working...yet -->
<audio
class = "audioPlayer uniqueShowAP"
controls <--controlsList="nodownload"
src="/shows/static/shows/{{ song.song_location }}">
Your browser does not support the
<code>audio</code> element.
</audio>
{% endfor %}
What I want to happen is for every concert that I save it will go through the songs saved under that concert and add them to under the name of the song. I have each song's relative location saved in the data base so that I should be able to just call upon it's location.
I've tested this out by calling {{ song.song_location }} which correctly displays each song's location.
I also tried using src="{% static 'shows/audio/redRocks2019/01 Yi.mp3' %}" just to make sure that the element is at least set up correctly and that works too.
How do I format the src="" in the element to point it to the correct audio files? I can include my views and models or whatever additional information you might need to help me figure this out. I've been at this for a while now and just need someone to point me in the right direction.
I think you should look into using a FileField. You can have a Song model, with a song_file = FileField(upload_to='song_files/') field. Then, the files can be saved and accessed more easily when looping through model instances:
<audio src='{{ song.song_file.url }}' controls></audio>
These files will be stored locally per your media settings. You can easily transition to using a storage solution like S3 for your media files, as you mentioned. It works incredibly smoothly with Django, so don't be scared.
Static files should be for things like css, js, and images that aren't going to change, like a favicon perhaps. I guess it could be used for a static song, hypothetically. But thinking about using static files this way above in conjunction with a model instance is a bit contrived and confusing, I think.

How to configure Atom to autocomplete Django templates

I need to find a way/package to make Atom use autocomplete for Django projects, especially Django templates.
I found this package in Atom's installer, but it doesn't include a shortcut for auto completion of this syntax {% %}, {{ }} which I need the most.
Any help will be appreciated
You could make your own snippets in Atom.
To do that go to Edit > Snippets
In the document that open's you can paste this bit:
'.html.django':
'Example snippet':
'prefix': '%%'
'body': '{% $1 %}$2'
This example would expand to {% %}, placing your cursor inside. To trigger it you type %% and hit tab. A second tab would place the cursor after the closing bracket.
The .html.django part means this snippet is active only in documents that are marked as HTML (Django)
I don't see why you would need a snippet for {{ }} as Atom auto-close's the brackets.
For more information read this - http://flight-manual.atom.io/using-atom/sections/snippets/
Disable atom-django package
I installed the django-templates package
in the django-templates settings in the option Default to Django Templates For File Paths Containing add the value directory_name/
reload atom
When you create an html file inside templates folder the it recognizes it as html django template
I configured it and it works
I faced the same situation and bit explored more inline with #4140tm's comment and found atom-django-templates repo. One can follow the steps given in the repo to have autocomplete in Atom editor
OR please follow below steps that I followed on my Mac.
How to Do it
1. Open "/Users/YourAccountName/.atom/snippets.cson"
2. Copy content from django-templates.cson
3. Paste it in "/Users/YourAccountName/.atom/snippets.cson"
4. Open any .html file and hurrah.... You are ready to Rock.
How it looks

jinja2 flask escaping jinja2-highlight

I need to render some code in a web page, with highlight. I'm using Flask and found that I could use jinja2-highlight. It works great, but I have some problems.
What I tried :
{% highlight 'python' %}
{{ item.text }}
{% endhighlight %}
Seems to work, I have my 40 lines of code but all special char like &#34 ... are displayed as this. So I add the |safe to the line {{ item.text }}. And now, all char are ok, but I don't see my full code, only 5 lines (the fifth line is complete).
I think I know what is the problem but don't know hw to solve it.
I have some line in my code like or and it seems that it's not escaped. Any idea why jinja2 does not escape <> ?
I'm not clear about security question with the |safe too. What does the server risks ?
item.text|safe should help. It appears that you're escaping the code twice. You may have some kind of auto-escape enabled.
You don't have to care about the server safety, as long as you don't share your actual code of your site.

django template: include a *relative* file but don't parse it

How can one include a file in a django template like {% include %} would, but without parsing the file's contents ?
I am aware of {% ssi %} but that last one would not accept relative paths and throws '[Didn't have permission to include file]' at me.
EDIT: this is NOT a duplicate of How can I tell Django templates not to parse a block containing code that looks like template tags? . As I commented here, I need a directive to include a whole file, not a directive to ignore a block inside a template.
Note: I'm trying to include angularJs templates which are in the project's directory, but the syntax conflicts with Django template
If you don't want to have to modify the files you're including, it looks as if the only way to do this is with a custom template tag.
Fortunately, it looks as if someone else has already posted one called include_raw on djangosnippets.org, although it was written for an older version of Django, so you'll have to make some modifications along the lines of those mentioned in one of the comments below the snippet.

'markup_tags' is not a valid tag library

Everytime I add a particular object to my database (this only affects one particular app on my site), I get the TemplateSyntaxError 'markup_tags' is not a valid tag library.
Specifically, it cannot load this: {% load markup_tags %}. But this is strange because when I open a Django shell and import markup all is fine.
This question has been asked before on StackOverflow here: Django markup templatetags error
However, I don't understand why this is happening and I don't really understand how to fix it. They said render_to_response the template. But I'm not sure exactly how I would do this from the shell (Django noob here).
So the problem was that I was actually not properly importing the module into into my project that contained markup_tags. While the library was available in my environment, in my settings.py I did not properly reference it. Dumb mistake, but might help someone else.
Consider trying {% load markup %} in your template.

Categories

Resources