Closed DawidPL closed 4 years ago
Hi Dawid,
django_distill
's distill_func
's are just for the URL part of views. For example, if you have a blog the blog post with the URLs such as :
path('/blog/<int:id>_<slug:title>.html', blogpost_view)
So once you've added some blog posts into an example BlogPost model you would have URLs like:
/blog/1_some-post.html
/blog/2_some-other-post.html
/blog/3_some-extra-post.html
What a distill_func
needs to do in this blog example is iterate over all blog posts in the database. You can view it from the browser down, so to generate all the blog posts statically you effectively need to call each URL in Django, render it, save the output HTML to disk. So a distill_func
might look like:
def iter_blog_posts():
for post in BlogPost.objects.all():
# What we need to return here is an iterable where each value
# matches the URL pattern, which is "post id" then "post slug"
# from the URL path(...) pattern
yield (post.pk, post.slug)
and then use it in urls.py
, replace path
with distill_path
and add in the distill_func:
...
distill_path('/blog/<int:id>_<slug:title>.html', blogpost_view, distill_func=iter_blog_posts)
...
Going back to your example code, you are viewing this the wrong way. The end result you want to generate is a static file with HTML in it. This has no relation to the models and data used in your view. Your URL, /home.html
contains no dynamic values at all, so your distill_func
would be:
def do_nothing():
return None
If you like you could just use distill_func=lambda x: None
as shorthand for "do nothing".
If for example, you want to change the language of your page depending on cookies or something, then obviously this makes no sense in a single flat file as you can't store multiple languages in a single static HTML file. If you want to alter the content based on language, for example, you would need to generate like /main-en.html
, /main-de.html
, one for each language etc.
In summary, distill_func
literally just maps URL arguments for you views, you can do whatever you normally do in your view providing it returns static HTML. You do not need to modify your views at all to use django_distill
, you just edit your urls.py
. Hope this helps!
Hi, I try to understand how can I generate my project in static files. It has multiple class objects in views and to be honest I don't know how pass them to distill_func . For example my index looks like this:
How can I do that ?