source: asadb/util/db_form_utils.py @ 7f16c8e

space-accessstablestagetest-hooks
Last change on this file since 7f16c8e was 7f16c8e, checked in by Alex Dehnert <adehnert@…>, 14 years ago

Agghhh file naming.

So, it turns out if your file name overlaps with a file name in use by Django,
things break horribly, in ways that are confusing and result in mysterious
errors deep in Django's stack.

  • Property mode set to 100644
File size: 1.1 KB
Line 
1from django.forms.widgets import Widget, HiddenInput
2from django.utils.safestring import mark_safe
3from django.utils.html import conditional_escape
4
5class StaticWidget(Widget):
6    """Widget that just displays and supplies a specific value.
7
8    Useful for "freezing" form fields --- displaying them, but not
9    allowing editing.
10    """
11
12    def __init__(self, *args, **kwargs):
13        self.value = kwargs['value']
14        del kwargs['value']
15        super(StaticWidget, self).__init__(*args, **kwargs)
16        self.inner = HiddenInput()
17
18    def value_from_datadict(self, data, files, name, ):
19        return self.value
20
21    def render(self, name, value, attrs=None, ):
22        if value is None:
23            value = ''
24        # We have this inner hidden widget because that allows us to nicely
25        # handle a transition from StaticWidget to another widget type
26        # (e.g., the user loads a form while unauthenticated and submits while
27        # authenticated)
28        hidden_render = self.inner.render(name, value, attrs=attrs)
29        return mark_safe(conditional_escape(value) + hidden_render)
Note: See TracBrowser for help on using the repository browser.