I'm new to Django, and I'm pretty sure I've read or heard about a way to do this, but I can't find it anywhere.
Rather than sending the rendered output from a template to a browser, I want to create an html file that can then be served without the need to go through the rendering process every time. I'm developing on a system that's separate from our main website's server, and I need to make periodic snapshots of my data available to our users without giving them access to the development system.
My intuition says that I should be able to somehow redirect the response to a file, but I'm not seeing it in the docs or in other posts here.
You can leverage Django's template loader to render your template, including whatever context you pass to it, as a string and then save that out to the filesystem. If you need to save that file on an external system, such as Amazon S3, you can use the Boto library.
Here's an example of how to render a view to a file, using an optional querystring parameter as the trigger...
from django.shortcuts import render
from django.template.loader import render_to_string
def my_view(request):
as_file = request.GET.get('as_file')
context = {'some_key': 'some_value'}
if as_file:
content = render_to_string('your-template.html', context)
with open('path/to/your-template-static.html', 'w') as static_file:
static_file.write(content)
return render('your-template.html', context)
django-bakery is a well-developed "set of helpers for baking your Django site out as flat files".
There's a well developed Django app specifically for this: django-render-static
Related
I have an angular application that is gona be installed on many sites (owner requeriment) but, for every site, the configuration must be different (colors, some texts, some logos, backend server, etc). I want to write a configuration file that will be read by the angular app and then apply that configuration, I will put the file in every site and when I make a change to the backend server, any configuration text or other configuration I will only change the configuration file and the angular app will work transparently.
How can I achieve this?
I was thinking in writing a json file and read it from angular. Is this a good practice?
you can use 'environment.ts' for your problem.
but I use 'environment.ts' or 'environment.prod.ts' for the Variables that have different value in product and develop mode.
for values like store/app/crm name, simply you can add 'ts' file like 'myAppConfig.ts' in 'src' folder or 'app' folder.
then declare your variables like this:
export const myAppConfig = {
appName:'samuels online store',
expireDate:'2023/30/12',
customerName:'samuel l jackson'
};
then you can use it,like this:
first import it in your 'ts' file, for example in 'app.component.ts'.
import { myAppConfig } from 'src/myAppConfig';
then use the values of it, like this:
customerName = myAppConfig.clientName;
finally, you can put or change values in 'myAppConfig' from your backend and give a fresh version to your new customers.
have a good time!
I'd like to use a .json file to load all of my content copy into my new Vue web site.
I'd like to import a file:
import Copy from '#/content/home.json';
Then have the content accessible to my template. My preference is to have the content file loaded in the page, then pass relevant data into components as props.
I hope that makes sense, my searches have pulled up all sorts of JSON/data related solutions but they all seem over-engineered for what I want.
If you need more info I will happily provide.
Suppose I have assets/data/geo/regions.json file in my NUXT.js project folders structure. How can I read data from this file into my project?
I have tried axios but I don't know what URL will have this file, I have tried all possible URLs. What is the better solution to do that? Maybe better to hold JSON files in static folder?
Thanks!
If the regions.json file won't change, you can easily put it in the static folder.
Then the url will be /data/geo/regions.json
See this question on the nuxt issues page
You can import JSON files with import data from 'data.json' and use the data property straight in your component.
You may want to use "require" instead of "import" if you planning to load data within the loop.
jsons = ["json_one","json_two"]
jsons_readed = []
// In the loop
file = require(`./assets/data/geo/${jsons[i]}`)
jsons_readed.push(file)
Then I think you can use jsons_readed to access objects.
You can use Nuxt Content for that:
Empower your NuxtJS application with #nuxt/content module: write in a content/ directory and fetch your Markdown, JSON, YAML, XML and CSV files through a MongoDB like API, acting as a Git-based Headless CMS.
The basics are as easy as the following line. That will load the regions.json file, parse it, and store its content in the content variable. See Nuxt Content's documentation for more information about it.
const content = await this.$content('regions').fetch()
Alternatively you can read our blog post about
Using Nuxt Content with a JSON File. It describes how to extend existing pages with JSON content but also how to dynamically generate pages based on it.
Disclaimer: I work at FrontAid CMS.
I am trying to use Node.js to implement Data Scrawling. I used axios to GET HTML file and use cheerio to get data.
However, I found that the HTML doesn't return with data but only layout. I guess the website with load the layout first, then doing ajax things to query data then rendering.
So, Anyone know how to GET the full HTML with data? Any library or tools?
Thanks.
i would suggest you to use selenium library with bs4 library in python if have some experience on python.
for node
https://www.npmjs.com/package/selenium-webdriver
i have written scraper in python using both library.
scraper is for linked in profile which take name from excel file and search if data available add it into another excel file
https://github.com/harsh4870/Scraper_LinkedIn
for node code goes like
driver = webdriver.Firefox();
driver.get("http://example.com");
html = driver.getPageSource();
Im trying to develop a website with some users who can write blogs.
In this site users need to be able to save their template created by smarty or twig to db and then i will render that from db.
Any one have any idea about how i render templates from database?
http://www.smarty.net/docs/en/resources.string.tpl
load template from database ->into string -> render.
don't forget about caching and security though
it's harder to do with a twig (because of twig's inheritance), but - in a limited way - possible .