Flask non-template HTML files included by Jinja - python

In my Flask application, I have one html file that holds some html and some js that semantically belongs together and cannot be used separately in a sensible way. I include this file in 2 of my html templates by using Jinja's {%include ... %}.
Now my first approach was to put this file in my templates folder. However, I never call render_template on this file, so it seems unapt to store it in that directory.
Another approach would be to put it into the static folder, since its content is indeed static. But then I don't know how to tell Jinja to look for it in a different directory, since all the files using Jinja are in the templates folder.
Is there a way to accomplish this with Jinja, or is there a better approach altogether?

You're over-thinking this. If it's included by Jinja, then it's a template file and belongs in the templates directory.

Related

How To Render An HTML With Css + JavaScript File In Django

Hay,
I Have An HTML with Css + JavaScript Page When I Try Rendring This Page In Django Like This:
def about(request):
return render(request, 'about/index.html')
but I Only Get The Html Content Without The Css And JavaScript.
I Thought This Might Be Because Of The Statics.. So I Run:
python manage.py collectstatic
But I Get This:
0 static files copied to 'C:\Users\Ammar\Desktop\Python Learning\vidly\static', 128 unmodified.
What Should I Do,
I Wish Someone Can Help.
Django has a feature of managing a good directory structures
i.e. project->apps->templates/views/urls
In a Django project all static files example images,css files,(designing part) is stored in a directory named static.
This helps in leveraging the same static properties over several apps and keeping the code structure clean.
The collect static command helps to get all static files present in the project to a static folder and we need to mention the static path in settings.py file .
Django documentation might clear your doubts further I am putting link here that might help you .
https://docs.djangoproject.com/en/3.2/howto/static-files/
What's the point of Django's collectstatic?

Django - How to make reference to file in static files to a file in static files

I want to refer, let's say, an image, or a font in my static files to my style.css file which is also located within my staticfiles. How do I make this reference, since I can't load staticfiles within the staticfiles? Does staticfiles have a specified path to reach it or is there a way for me to set a path for it in order to access it. The other questions I saw that were alike this one were more for javascript files and found alternatives to it, which would not be viable in this case since I want to perform it on a cascading style sheet.

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.

Django return .html file directly without parsing for template tags at all

I'm writing a single view Javascript application with a Django backend that only needs to return the initial index.html and all other templates come from a CDN. My problem is that this first index.html file is parsing out some of my "{{}}" handlebars which I wanted to leave for the JS library to interpret.
I DO NOT want to use 'verbatim' or 'raw' or any additional tags because I don't want any django specific stuff in my static template files.
A possible alternative answer to this would be desmonstrating how to to make your inital index HTML response also come from the CDN but I didn't think that was possible.
If you don't want to render a template, simply don't render it. Django won't render anything unless you specifically call template.render or one of the shortcuts.
If you just want to return an HTML file, you could just open it as a normal file, read it, then return the content as the response.
Alternatively, as suggested in the comment, you can serve it as a static file.

What is the best way in python/django to let users add files to the database?

I am trying to let users create html pages that they can view on my website. So the homepage would just have a place for file upload, then some script on my site would take the file and convert it into the html page and place it at mysite.com/23klj4d(identifying file name). From my understanding, this would mean that the urls.py file gets updated to route that url to display the html page of the file. Is it possible to let users do this? Where would I put that conversion script?
You could simply write static files to disc, and then serve them as static files. That would be easier, but it depends on your other requirements.
But, from what I understand in your question you'd need:
A form to upload
A upload handler itself which inserts into the db
A view that renders based on a path
Not sure about the urls.py entry. You'll want something in there to separate this content from the rest of your site, and you'll probably also want something to safeguard against the file extensions that you serve there.
note: this has security hole written all over it. I would be super careful in how you test this.

Categories

Resources