已解决:没有提供异常消息的 django 模板

主要问题是提供的 django 模板无异常消息不是特定于 Django 的。 它可以与任何 Web 开发框架一起使用。

 I have a form with a field that is required. If the user submits the form without filling in the required field, I want to display an error message. This is my view: def add_item(request): if request.method == 'POST': form = ItemForm(request.POST) if form.is_valid(): item = Item() item.name = request.POST['name'] item.save() return HttpResponseRedirect('/items/') else: return render_to_response('items/add_item... Read More

Django - How to use a custom decorator with class based views? django-templates I have been trying to use a custom decorator with Django's class based views but it doesn't seem to work as expected and I can't figure out why? Here's what I'm doing: from django import template from django... Read More

How do you create a dropdown list in Django using Python? django-templates How do you create a dropdown list in Django using Python? For example, let's say that you have the following data stored in your database table called "Fruit": Fruit ID Fruit Name 1 Apple 2 Banana 3 Orange 4 Pear 5 Strawberry 6 Watermelon 7 Pineapple 8 Grapes 9 Kiwi 10 Mango 11 Papaya 12 Peach 13 Cherry 14 Grapefruit 15 Lemon 16 Lime 17 Cantaloupe 18 Honeydew 19 Rockmelon 20 Pomegranate 21 Apricot 22 Avocado 23 Blueberry 24 Blackberry 25 Cranberry 26 Fig 27 Gooseberry 28 Guava 29 Jackfruit 30 Lychee 31 Mulberry 32 Nectarine 33 Orange 34 Peach 35 Pear 36 Plum 37 Prune 38 Raspberry 39 Strawberry 40 Tomato 41 Pineapple 42 Banana 43 Mango 44 Orange 45 Strawberry 46 Cherry 47 Apple 48 Guava 49 Banana 50 Mango 51 Orange 52 Strawberry 53 Cherry 54 Apple 55 Guava 56 Banana 57 Mango 58 Orange 59 Strawberry 60 Cherry 61 Apple 62 Guava 63 Banana 64 Mango 65 Orang...Read More

Django – 如何使用 Python 在 Django 中创建下拉列表? django-templates 如何使用 Python 在 Django 中创建下拉列表? 例如,假设您在名为“Fruit”的数据库表中存储了以下数据: Fruit ID Fruit Name 1 Apple 2 Banana 3 Orange 4 Pear 5 Strawberry 6 Watermelon 7 Pineapple 8 Grapes 9 Kiwi 10 Mango 11 Papaya 12 Peach 13樱桃 14 葡萄柚 15 柠檬 16 酸橙 17 哈密瓜 18 蜜瓜 19 哈密瓜 20 石榴 21 杏 22 鳄梨 23 蓝莓 24 黑莓 25 蔓越莓 26 无花果 27 醋栗 28 番石榴 29 菠萝蜜 30 荔枝 31 桑葚 32 油桃 33 橙子 34 桃子 35 洋李子 36 37 梨子 38覆盆子39草莓40番茄41菠萝42香蕉43芒果44橙子45草莓46樱桃47苹果48番石榴49香蕉50芒果51橙子52草莓53樱桃54苹果55番石榴56香蕉57芒果58橙子59草莓60樱桃61苹果62番石榴63香蕉 64 芒果 65 红毛猩猩……阅读更多

如何在 Django admin 中使用外部模板文件? django-templates 我正在尝试为我的 Django 管理员的 change_form.html 页面使用外部模板文件。 文档说:要覆盖 change_form.html 模板,只需创建一个具有相同名称的新模板并将其放在任何一个 TEMPLATE_DIRS 目录中。 但我不确定将它放在哪个目录中。我试过将它放在 /admin/change_form.html、/admin/change_form/、/admin/change/、/admin/chang/、/admi 下……阅读更多

异常信息

当 Django 中发生异常时,它会向控制台打印一条消息。 此消息通常包含有关异常的信息,例如导致异常的 Python 代码。

修改模板

有几种方法可以在 Django 中修改模板。 最简单的方法是使用模板标签。 例如,要将标题添加到模板:

{% 加载标头 %} {% set header = “我的标头” %} {% if not is_page %} {% set header = “我的标头” %} {% endif %}

您还可以使用上下文处理器来更改模板变量的上下文:

{% 加载上下文 %} {% 设置上下文 = “my_context” %} {% 如果不是 is_page %} {% 设置上下文 = “my_context” %} {% endif %}

相关文章:

发表评论