Snippet List
A minimally invasive Select widget that looks and acts like a disabled select input, but still submits with the form. It works by rendering the form as disabled, and supplements it's output with a hidden input field with the same name and value.
Tested in Django 3.0, probably works in Django 1.11+
The simplest way of displaying a "details" table about any model, is to show a ModelFrom with all fields readonly or (selects) disabled.
Also, the labels are preferably translatable, not just capitalized names of the column tables in your models. So the constructor translates the field labels as well.
These decorators can be used to make some model/form fields readonly.
**Sample usage:**
# Use this decorator on form with readonly fields.`
@modelform_with_readonly_fields`
class FooAdminForm(forms.ModelForm):`
...
# This decorator shoud be used to protect selected fields `
# from modification after initial save.`
@has_readonly_fields`
class Foo(models.Model):`
read_only_fields = ('name', )`
...
**Result** will be the same as shown in this post: [Readonly field](http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-be/1424453#1424453) and
[Readonly model field](http://www.djangozen.com/blog/read-only-fields-in-models)
- forms
- model
- field
- readonly
This code defines a decorator that inform users of your site when the Google AppEngine data store is in read-only mode during maintenance periods.
Use it as a decorator on your views that require write access to the data store.
@requires_datastore_write
def update(request):
...
Create a `maintenance.html` Django template (or change the name in the code) with the message that the user will see, something like:
This application is currently in maintenance mode and some operations are temporarily unavailable.
Thanks for trying back later. Sorry for the inconvenience.
- decorator
- readonly
- appengine
- maintenance
- capabilities
- datastore
A Form and ModelForm which provides the ability to specify certain fields as readonly, meaning that they will display their value as text wrapped with a <span> tag. The user is unable to edit them, and they are protected from POST data insertion attacks.
The recommended usage is to place a NewMeta inner class on the form, with a readonly attribute which is a list or tuple of fields, similar to the fields and exclude attributes on the Meta inner class.
class MyForm(ReadonlyForm):
foo = forms.TextField()
bar = forms.TextField()
class NewMeta:
readonly = ('foo',)
Use these forms as you would a standard form in your templates.
- forms
- field
- readonly
- modelforms
- span
This replaces the html select box for a foreign key field with a link to that object's own admin page. The foreign key field (obviously) is readonly.
This is shamelessly based upon the [Readonly admin fields](http://www.djangosnippets.org/snippets/937/) snippet. However, that snippet didn't work for me with ForeignKey fields.
from foo.bar import ModelLinkAdminFields
class MyModelAdmin(ModelLinkAdminFields, admin.ModelAdmin):
modellink = ('field1', 'field2',)
- admin
- field
- widget
- readonly
- modelchoicefield
Put this code and import it where you define your ModelAdmin-classes.
# typical admin.py file:
from django.contrib import admin
from foo.bar import ReadOnlyAdminFields
class MyModelAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
readonly = ('field1', 'field2',)
- django
- admin
- field
- readonly
This is a little base class that I use to display forms in a readonly mode. Providing a simple javascript on the front end to allow a user to click an **edit** link that will show/hide the actual form.
To use it simply inherit from it. Then in your templates do the following:
`{{ form.as_readonly_table }}`
**Other Notes**
* **form.html**
`{% for field in bound_fields %}
{% include "newforms/field.html" %}
{% endfor %}`
* **field.html**
`{% if field.errors %}
<tr>
<td> </td>
<td>{{ field.errors }}</td>
</tr>
{% endif %}
<tr>
<th style="text-align:right">
<label for="id_{{ field.name }}">{{ field.label }}{% if field.field.required %}<span class="required"><sup>*</sup></span>{% endif %}</label>
</th>
<td>{{ field }}
{% if field.help_text %}
<p class="helptext">({{ field.help_text }})</p>
{% endif %}
</td>
</tr>`
- template
- newforms
- readonly
- edit-mode
10 snippets posted so far.