Django - How to setup dynamic templating within django - django

So I am having this major issue regarding the templating setup. I have a model that is created and attaches html, JS, and CSS files on upload to a corresponding folder with the model name. The user is able to upload these files (HTML,CSS,JS), and I need to be able to access the HTML file within this weird location.
/template/app/app.html (the location where the view leads, which idc about)
/media/uploads/modelName1/base.html ( I want to dynamically link each view with their corresponding model folder )
/media/uploads/modelName1/style.css
/media/uploads/modelName1/base.js
for the view I made it so the HTML file of the model is passed within the template and then I can use {% include %} but obv that can't work b.c the HTML included isn't even part of the template. If you have any suggestions on how to approach this problem, it would be much appreciated.

Related

Single page application using django-Nav Bar for django app

In Angular,we can use routing to create SPAs . So I can have a navigation bar that's common for all templates(or written only once). Basically only required part of the page loads everytime .
But in django,so far I haven't seen anything like it. Do i have to include the code for the Nav bar in each template file?
You just have to write single time in any html file you can name whatever you want for example _base.html is the file which contains navbar code then i want to insert in every other html file then i just have to enter tag {% extends '_base.html' %} at the top of the other files and that will load the _base.html there as well. If you need more relevant code you can consult that repository as well https://github.com/wsvincent/djangox

How to use a base.html for default Wagtail Page instance?

Wagtail starts new projects with a single welcome template. However, this template does not inherit from any parent template, such as a base.html. In our project, we have previously defined a base.html containing global HTML tags and CSS/JavaScript includes.
How can we tell the default Wagtail Page model to display in a template that extends base.html?
To clarify, there are two possible setups, depending on how you create your project:
If you start a new project from scratch with wagtail start myproject, the initial migrations (which exist partly within Wagtail itself, and partly within the project template) will set you up with a HomePage model and an initial homepage. The template for this page type lives at home/templates/home_page.html, and is already set up to inherit from a base template (which lives at myproject/templates/base.html).
If you follow the documentation for integrating Wagtail into an existing Django project, the initial migrations (which in this case are entirely within Wagtail) will give you an initial page of type wagtailcore.Page. You're not really expected to use this page type directly (not least because it has no fields beyond the title, and no way of adding new ones) - it's only done this way because there are no "proper" page models set up yet, and there needs to be something as the initial state of the page tree. The idea is that once you've set up at least one page model within your app, you can create your real homepage, point the Site record at it (in Settings -> Sites), and delete the initial stub one.
Nevertheless, if you really want to give wagtailcore.Page a full-fledged template with a base template, you could create a template file inside one of your project's apps at the path templates/wagtailcore/page.html. As long as the app in question is above 'wagtail.core' in the INSTALLED_APPS list, this will override the barebones template supplied by Wagtail.
{% extends "base.html" %}
Add that to your template, it will extend anything in the base.
If your base template has blocks in it, you use the same syntax
{% block content %}
This will appear wherever you have this same block in the base.html template
{% endblock %}
Wagtail is built on top of Django, you can read more about its templating language here: https://docs.djangoproject.com/en/3.0/ref/templates/language/

Django Show Files on Disk

I want to have a page that will show files from a particular folder on the disk and have the user select one and then "pass" the file path of that file to another view for further processing. I have no idea where to start, all of my searching turns up uploading files.
If you only need to display files from one directory on disk, perhaps a simpler solution would be to use the built-in Django FilePathFeld. It will return a path as the value and is represented by a select in forms.
This field isn't for uploading a file, it's only for selecting a pre-existing file.
The only project i know that is still maintained is django-filebrowser
The extension is pretty flexible, you can choose to use paths relative to a storage location or absolute server paths.
You should be able to pass the file path to another view by adding the view's path to your form action attribute like this:
<form name="form" method="post" action="/path/to/view">
...
</form>

Django: using {{STATIC_URL}} from python side

Building a "history" system that serves static pages based on the date the user asks for. Not all dates have associated pages, and it isn't possible to know, based on what's in the database which do, which don't. I haven't been able to find a way to redirect to a static page because there doesn't seem to be any way to capture the value of the {{STATIC_URL}} tag on the python side. I have got some code that depends on the static file being on the same file system as the django server, but that is clearly wrong. I have two needs:
1: how can I (redirect?) to the static page(s) from my views.py file?
2: how can I query for the existence of a particular one of those static pages?
I'm not certain from your question if you understand the intention of the {{ STATIC_URL }} tag. It is a URL prefix for static content files such as css, javascript, or image files. It is not intended as a path for your own static HTML files. In the end I'm not sure you are asking the right question for what you are wanting to accomplish.
As zzzirk says: Django really isn't about flat pages, so the most Djangonic solution is to put flat pages beside the django app, not within it.

Render a block through a template that will be added to an existing page as well as it's own

I have a page which is for album/picture management with 2 sections: Albums and Pictures.
When an album is selected the pictures block needs to change via AJAX to reflect the album selected.
This means the rendered pictures block needs to be provided to the Albums page as well as be available as it's own View for the AJAX source.
I understand I could solve this by making the pictures block always render from AJAX even when the album page loads, however I would like to deliver the default album pictures within the initial page load if possible. In order to do that, I'd like to render the pictures block via the same template in the Album page view as is used for the Picture AJAX View.
I'm only familiar with providing templates as a template_name property within an TemplateView object.
I guess I could simply call an instance of PictureView using inclusion_tag, and pull the data I need out of the render_to_response (I haven't tried this yet, I'm just theorizing) however that seems a bit dirty to me. I'm wondering if there's a more elegant solution to this problem?
Thanks!
jQuery, Django templates, JS templating and backbone.js should tie this together.
I would suggest having a dedicated template for the Pages block. Include this template in the Django template for your page.
For dynamic updates use a JS templating library such as included in underscore.js or moustache.js. You can change the template demlimiters used so that they are the same as djangos.
Include the raw Pages block template into a javascript template block - use the django ssi tag.
Use django-tastypie to set up an api that returns the data for Photos as JSON. Your template library can then use data to fill in the template in the JS template block and you can then replace the Photo block with this rendered HTML. Use of backbone.js should ease the tying of DOM elements and JS events.
If I understand your question correctly, I did something similar once with the subsection having its own template file, which only describes that one section of the page. I used the include tag to include it into the page template, so it loaded when the page did and then rendered that template with updated values and replaced it on the page with AJAX when the content was meant to change.
Is that an option for you?