I'm creating a web based application in Django. The app will generate multi-page pdf reports with Latex. Currently I'm generating the tex files using Jinja2. Since my web app is using the Django template engine, I thought I might as well use it to generate the tex file that will be sent to pdflatex. In Jinja2 I changed the block_start_string and block_end_string from {% and %} to {# and #} respectively. I did this so that it plays nicely with Latex.
My question is, can I change block_start_string to {# in Django, but only for the part of my code that needs to generate the tex files? My html templates should still use {% and %}. Jinja2 has a concept of environments, not sure if Django also has?
Django does not currently support changing the block start or end strings, however Django does support using Jinja2 as a template backend. This way, you can use your existing templates.
https://docs.djangoproject.com/en/1.9/topics/templates/
Related
Problem description
I am starting working on a Django project and the server-side rendering template is a little hard to work with. Usually I develop the front-end application with hot module reload server so that I can see the rendered page during development.
However, for Django project I can only view the site by serving and open from browser. If I open the template directly, the include, extends are not processed, hence CSS and JavaScript are not loaded in the base template.
My question is, is there any tool or workflow that can help develop the Django template in offline so that the style and layout can be rendered properly during development?
Edit: justification
As comment mentioned there are some plugins that supports reload the Django page. However, I would like to know whether it is possible to work with the template HTML totally off the Django server, i.e. work with the html static page? There are some scenarios where I feel it is not suitable:
A page that refreshes slowly: e.g., slow database query before the page can be rendered.
A template that is not accessible normally: e.g., a part of html inside {% if %} that is not normally accessible, such as an error message.
A template that is not yet registered in the urlpatterns routes.
Thank you
In my current django project I have the following model:
Python 3.7.1 / django 3.0
class SampleClass(models.Model):
active = models.BooleanField(default=False)
And the following template code with 'sample' as an instance of the SampleClass above (only small snippet):
HTML / django template language
<form action='#' method='post'>
<input type='checkbox' name='is_active_checkbox' {% if sample.active %}checked{% endif %}>
</form>
Now, when the state of the checkbox is changed, I would like to immediately apply the change to the django database, without reloading the page (if possible). Is there any way to do that?
I would recommend you to start with a basic AngularJS, there are several functions that you can apply quickly, this option allows you to just add a CDN and keep working the frontend from Django.
If you want to do something more robust, you can apply Angular 8+. Here you should use Angular as a web server, and Django as REST Apis.
Here are some basic examples, where you can adapt it to your needs and play around with the theme. I've been working with Django for more than 6 years, but I started to integrate it with Angular less than a year ago (you can choose React, Vue, or whatever suits you best, but Angular was a comfortable decision for me)
I leave you here my Github with several examples of Django with all its code, there is also 1 pair with React.
Github/Django
I am a Laravel developer and used Vue as my frontend framework in the past.
However, I got interested in learning Python, specially its web framework equivalent - Django. Compared to Laravel, Django is pretty much way better in all aspects - it's fast, it's one of those 'all-in-one' type of framework, plus it's PYTHON. A 15-minute task in Laravel can be accomplished in Django for 5 minutes.
One thing that's just bugging me is how to integrate Vue on its templates. I tried the normal CDN method but it doesn't actually work. I figured maybe because django's syntax -{{ sample variable here }} and Vue's are the same. I tried writing delimiters like delimiters: ['[[', ']]'], but it just made it more complex and still will not work.
I tried doing the webpack method which install a separate vue project within the django project, then used several (like a lot of third-party plugins) to somewhat connect the vue project, to the django project. It is so tedious on its process and it seems like it's not the why it should be. I kinda miss it on Laravel where you could just create an app.blade.php main template, import app.js on it, then just take it from there.
Is there a way to just integrate Vue on django's templates quickly like you can on Laravel?
We use Vue with Django via cdn, and it is pretty easy to set up as you can do everything in the template file.
You have to be sure to import Vue via script tag somewhere on the page and then either define your own delimiters (as you've done) or use the {% verbatim %} {% endverbatim %} tags around any variables you do not want django to try and evaluate. You can either write your Vue function in a script tag on the page or import an external file via script tag the way you would import any other js file into a django template. If you posted your template code, I might be able to provide more guidance.
I'm using AppEngine and django forms passing template_values and using base.html with {% extends "base.html" %} this works well in a testing environment when I have one big form for adding entities.
I'm good in python, good with the datastore, but my html is weak/OLD
I want to have a step by step process for creating datastore entities. In broad strokes, what are some common methods/search-terms I should be using? I've heard that I might be hitting a bit of a limit at some point by using django instead of some other framework - am I already at that point?...can't be?...
Sounds like you are looking for Django Form Wizard?
http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/
I want to add an templatetag to the django admin, how can I go around extending the existing tags without needing to fudge in django.contrib.admin.templatetags?
Update:
Using {% load mytemplatetags %} in the admin templates breaks my server for some reason (im using nginx and throws me a bad gateway for that page). But the file mytemplatetags.py does exist in the templatetags module of my app.
Interestingly enough when I just misspell or apply a non-existing name for it, forinstance say {% load footemplatetags %} it just gives me an ordinary django error:
'footemplatetags' is not a valid tag library
So it probably knows mytemplatetags.py's existance, but doens't know how to handle it.
I am using django 1.3 alpha (just checked out the svn), perhaps its a good idea to get with something stable?
Nevermind, my template tag was broken.