在我的Django模板中,如何显示复选框和通知类型?

2 投票
2 回答
1338 浏览
提问于 2025-04-16 09:37

在Django_notification这个项目里(链接在这里:https://github.com/pinax/django-notification/blob/master/notification/views.py),我想知道怎么显示复选框?这里好像没有表单对象。通常我都是这样做的:{{ myform.thefield }}

@login_required
def notice_settings(request):
    """
    The notice settings view.

    Template: :template:`notification/notice_settings.html`

    Context:

        notice_types
            A list of all :model:`notification.NoticeType` objects.

        notice_settings
            A dictionary containing ``column_headers`` for each ``NOTICE_MEDIA``
            and ``rows`` containing a list of dictionaries: ``notice_type``, a
            :model:`notification.NoticeType` object and ``cells``, a list of
            tuples whose first value is suitable for use in forms and the second
            value is ``True`` or ``False`` depending on a ``request.POST``
            variable called ``form_label``, whose valid value is ``on``.
    """
    notice_types = NoticeType.objects.all()
    settings_table = []
    for notice_type in notice_types:
        settings_row = []
        for medium_id, medium_display in NOTICE_MEDIA:
            form_label = "%s_%s" % (notice_type.label, medium_id)
            setting = get_notification_setting(request.user, notice_type, medium_id)
            if request.method == "POST":
                if request.POST.get(form_label) == "on":
                    if not setting.send:
                        setting.send = True
                        setting.save()
                else:
                    if setting.send:
                        setting.send = False
                        setting.save()
            settings_row.append((form_label, setting.send))
        settings_table.append({"notice_type": notice_type, "cells": settings_row})

    if request.method == "POST":
        next_page = request.POST.get("next_page", ".")
        return HttpResponseRedirect(next_page)

    notice_settings = {
        "column_headers": [medium_display for medium_id, medium_display in NOTICE_MEDIA],
        "rows": settings_table,
    }

    return render_to_response("notification/notice_settings.html", {
        "notice_types": notice_types,
        "notice_settings": notice_settings,
    }, context_instance=RequestContext(request))

2 个回答

0

你展示的这个视图只负责处理发送过来的通知,而不是在页面上显示一个表单。你可以自己创建一个表单,里面加一个叫做 form_label 的字段,然后把这个表单放到任何一个发送数据到这个视图的页面上(这个字段可以设置为隐藏输入)。

3

这个视图的默认模板已经上传到github上,你可以在这里找到:https://github.com/pinax/pinax/blob/master/pinax/templates/default/notification/notice_settings.html

更新:Pinax已经移除了他们的主题,最后一次上传的模板可以在这里找到。

通知设置对象没有定义表单,所以复选框元素(以及整个表单)是用原始的HTML代码创建的:

    <form method="POST" action=""> {# doubt this easy to do in uni-form #}
        {% csrf_token %}
        <table class="notice_settings">
            <tr>
                <th>{% trans "Notification Type" %}</th>
                {% for header in notice_settings.column_headers %}
                    <th>{{ header }}</th>
                {% endfor %}
            </tr>
            {% for row in notice_settings.rows %}
                <tr>
                    <td>{% trans row.notice_type.display %}<br/>
                        <span class="notice_type_description">{% trans row.notice_type.description %}</span>
                    </td>
                    {% for cell in row.cells %}
                        <td>
                            <input type="checkbox" name="{{ cell.0 }}" {% if cell.1 %}checked="yes"{% endif %}/>
                        </td>
                    {% endfor %}
                </tr>
            {% endfor %}
            <tr>
                <td><input type="submit" value="{% trans "Change" %}" /></td>
            </tr>
        </table>
    </form>

撰写回答