django-templates

Render Django template from command line, without settings

岁酱吖の 提交于 2021-02-07 08:54:33
问题 Is there a way to render a Django template from command line without invoking any settings? I want to do this outside any Django apps or project, to be able to use it as a command line tool to render a template with some variables. Is there a tool that does this already? Jinja2 would be fine too. 回答1: You can use settings.configure() if you don't have any custom settings to configure. from django.conf import settings settings.configure() from django.template import Template, Context Template(

Django how to enter multiple records on one form

冷暖自知 提交于 2021-02-07 08:00:27
问题 I am writing a calendar app with the following models: class CalendarHour(models.Model): ''' Each day there are many events, e.g. at 10 am, the framer orders material, etc. ''' day_id = models.ForeignKey(CalendarDay) time = models.TimeField() work = models.TextField() def __unicode__(self): return 'work that took place at {work_time}'.format(work_time = self.time) class CalendarDay(models.Model): ''' Each project has so many daily logs. But, each daily log belongs to only one project

Django how to enter multiple records on one form

心已入冬 提交于 2021-02-07 07:58:22
问题 I am writing a calendar app with the following models: class CalendarHour(models.Model): ''' Each day there are many events, e.g. at 10 am, the framer orders material, etc. ''' day_id = models.ForeignKey(CalendarDay) time = models.TimeField() work = models.TextField() def __unicode__(self): return 'work that took place at {work_time}'.format(work_time = self.time) class CalendarDay(models.Model): ''' Each project has so many daily logs. But, each daily log belongs to only one project

Django TemplateResponse vs render

江枫思渺然 提交于 2021-02-07 05:22:58
问题 What is the difference between return TemplateResponse(request, self.template_name, context=context) and return render(request, self.template_name, context=context) Is there any scenario why I should use one of them and not the other? 回答1: A TemplateResponse delays the rendering of the template until after the view is finished. This allows any template response middleware to run on the response, and potentially alter the template or the context data before the template is rendered. After the

How to override django allauth email templates

这一生的挚爱 提交于 2021-02-07 03:01:25
问题 I'm using allauth version 0.35.0 and I want to override email templates to make my desired HTML template for them. No problem with allauth login and SignUp and etc. pages But I can't find any template for emails. It's just a .txt file in path /templates/account/email/ . But How can I set HTML message for allauth tasks like changing password and etc. for their email messages? Any help will be appreciated. 回答1: Ooops! Shame on me! in this part of allauth documentation This said that: Emails

How to override django allauth email templates

笑着哭i 提交于 2021-02-07 03:00:09
问题 I'm using allauth version 0.35.0 and I want to override email templates to make my desired HTML template for them. No problem with allauth login and SignUp and etc. pages But I can't find any template for emails. It's just a .txt file in path /templates/account/email/ . But How can I set HTML message for allauth tasks like changing password and etc. for their email messages? Any help will be appreciated. 回答1: Ooops! Shame on me! in this part of allauth documentation This said that: Emails

Custom Label in Django Formset

与世无争的帅哥 提交于 2021-02-06 11:41:04
问题 How do I add custom labels to my formset? <form method="post" action=""> {{ formset.management_form }} {% for form in formset %} {% for field in form %} {{ field.label_tag }}: {{ field }} {% endfor %} {% endfor %} </form> My model is: class Sing(models.Model): song = models.CharField(max_length = 50) band = models.CharField(max_length = 50) Now in the template instead of the field label being 'song' , how do i set it so that it shows up as 'What song are you going to sing?' ? 回答1: You can use

escape problem in django templates

青春壹個敷衍的年華 提交于 2021-02-06 10:48:28
问题 Let's say that I have this string: s = '<p>Hello!</p>' When I pass this variable to a template, I want it to be rendered as raw html. Looking at the docs I see that I can either use the safe filter: {{s|safe}} or disable autoescape: {%autoescape off} {{s}} {%endautoescape%} or inside the python code declare it safe: from django.utils.safestring import mark_safe s = mark_safe(s) None of these options are working for me. Whatever I do, the string is displayed as: <p>Hello!</p> I must be missing

Traversing multiple lists in django template in same for loop

为君一笑 提交于 2021-02-06 10:14:07
问题 I want to traverse multiple lists within a django template in the same for loop. How do i do it? some thinking link this: {% for item1, item2, item3 in list1, list2 list3 %} {{ item1 }}, {{ item2 }}, {{ item3 }} {% endfor %} Is something like this possible? 回答1: You have two options: 1. You define your objects so that you can access the items like parameters for x in list: {{x.item1}}, {{x.item2}}, {{x.item3}} Note that you have to make up the list by combining the three lists: lst = [{'item1

Is there a way to make a block optional in Django template

女生的网名这么多〃 提交于 2021-02-06 08:45:44
问题 In Django's templates system, if I have a block that I want to make optional using an if statement, how do I do it? I was trying this: {% if val %}{% block title %}Archive {{ foo }}{% endblock %}{% endif %} But that doesn't work. Is there a way to do that, so that for a given value (in this case Null) the block isn't issued and the base template uses the original values? Edit: Let me be a little more specific, so that it is easier to answer. I have a page with 10 entries per page. The user