How to generate a (html) file from a view in Django - django

I have a Django-driven website with several applications which are dynamically updated, but not too often, like "About", "Our services", etc. They have a two-level structure of pages, which I want to reflect in the top pop-up menu (see the picture).
I could make it as a custom simple_tag called each time from the base.html template. It would generate a menu each time a user opens or reloads a page, but think it is very costly since the app structure is changed seldom (say, once a year).
Instead, I want to like to generate an HTML code of the menu each time the app structure is updated. It will be stored as a statical HTML file, so my base.html will include the menu as a ready piece of code.
Are there recipes on how to generate an HTML file from the views (CreateView, UpdateView, DeleteView)?

You can use the render_to_string function from django that generated the html result from a template:
from django.template import loader
content = loader.render_to_string(template_name, context)
content contains the html result of rendering. You can add request object in the render_to_string function if needed.
you can find documentation about function here: https://docs.djangoproject.com/en/4.1/topics/templates/#django.template.loader.render_to_string

Related

Django - How to setup dynamic templating within 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.

What should be the correct way to store the scraped data for TextField

I want to Scrap data from websites lets say for instance SO.
For example on SO we write inside a Text Editor but on the backend it is stored in a Text Field with proper HTML markup and rendered whenever need. I am facing similar situation in which I have to scrap data for a Text Field so my question is how do I apply formatting before saving.
I am thinking of having a sample template and use Template engine like Mako to fill the context and then finally save it. Is this the correct approach. Maybe someone can share his experience.
If you are working with django already, why not using the framework templates?
Assume that you have a text_field_template.html in your templates directory, you can render it with the content in the model save method:
from django.template.loader import render_to_string
class MyModel(models.Model):
the_text_field = models.TextField...
def save(self,context):
self.the_text_field = render_to_string(text_field_template.html,context)
super(MyModel,self).save()
You can of course add a template arg, so you save(context,template).
It's better to render the template in the "save" method, since a typical DB activity has fewer writes than reads. If you save this field once, then read 10, or 100 times, it makes sense to render on saving.
You may have a different use case:
If the context is based on current and changing data, which is available only when the user reads the field, then you must render when pulling the record.
An application that uses the field once, one save - one read - one delete. It doesn't really matter if you render on the save or the read.
etc.
Note that from a safety point of view, you must escape everything before it goes to the DB.

how to display my own custome message in the top of admin page

want to display my own custom message and link in the top of localhost/admin/module/field before the select fields to change page. I tried with link_display that for displaying each instance obj itself, and i don't want that.
You need to override the change_list.html template for your app, see the documentation.
Money quote for your problem:
For example, if we wanted to add a tool to the change list view for
all the models in an app named my_app, we would copy
contrib/admin/templates/admin/change_list.html to the
templates/admin/my_app/ directory of our project, and make any
necessary changes.

Can all Django applications add items to a base template

I'm writing a webpage in Django and I have a application called "Website". This application defines some basic views and general layout of the whole site part of which is the navigation bar.
As I add applications responsible for different functionalities I would like to have them add links to that navigation bar. The links should appear not only when using a view from a certain application but in all others as well. The problem here is how to arrange this without the "Website" app knowing about all the other applications?
All Django apps inside one project can be aware of each other. For example, if you had an app called links, you could use models and functions from links to create new content, then use that content in Website. You will need to import the information. For example, to use links from links in your Website views:
# Website.views
from links.models import Link
def myView(request):
links = Link.objects.all()
# do something with the links
context = {'links': links}
return render(request, 'webpage.html', context)
Please excuse my syntax and stuff, I haven't tested this or anything. I hope this explains what you mean. Feel free to ask more, I'll do my best to answer.

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?