Skip to content

Commit 2046d91

Browse files
committed
new django code examples
1 parent 046bd12 commit 2046d91

20 files changed

+3110
-1
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
title: django.forms BaseForm Example Code
2+
category: page
3+
slug: django-forms-baseform-examples
4+
sortorder: 500011255
5+
toc: False
6+
sidebartitle: django.forms BaseForm
7+
meta: Python example code for the BaseForm class from the django.forms module of the Django project.
8+
9+
10+
BaseForm is a class within the django.forms module of the Django project.
11+
12+
13+
## Example 1 from django-wiki
14+
[django-wiki](https://github.com/django-wiki/django-wiki)
15+
([project documentation](https://django-wiki.readthedocs.io/en/master/),
16+
[demo](https://demo.django-wiki.org/),
17+
and [PyPI page](https://pypi.org/project/django-wiki/))
18+
is a wiki system code library for [Django](/django.html)
19+
projects that makes it easier to create user-editable content.
20+
The project aims to provide necessary core features and then
21+
have an easy plugin format for additional features, rather than
22+
having every exhaustive feature built into the core system.
23+
django-wiki is a rewrite of an earlier now-defunct project
24+
named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
25+
26+
The code for django-wiki is provided as open source under the
27+
[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
28+
29+
[**django-wiki / src/wiki / templatetags / wiki_tags.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/templatetags/wiki_tags.py)
30+
31+
```python
32+
# wiki_tags.py
33+
import re
34+
35+
from django import template
36+
from django.apps import apps
37+
from django.conf import settings as django_settings
38+
from django.contrib.contenttypes.models import ContentType
39+
from django.db.models import Model
40+
~~from django.forms import BaseForm
41+
from django.template.defaultfilters import striptags
42+
from django.utils.http import urlquote
43+
from django.utils.safestring import mark_safe
44+
from wiki import models
45+
from wiki.conf import settings
46+
from wiki.core.plugins import registry as plugin_registry
47+
48+
register = template.Library()
49+
50+
51+
_cache = {}
52+
53+
54+
@register.simple_tag(takes_context=True)
55+
def article_for_object(context, obj):
56+
if not isinstance(obj, Model):
57+
raise TypeError(
58+
"A Wiki article can only be associated to a Django Model "
59+
"instance, not %s" % type(obj)
60+
)
61+
62+
content_type = ContentType.objects.get_for_model(obj)
63+
64+
if True or obj not in _cache:
65+
66+
67+
## ... source file abbreviated to get to BaseForm examples ...
68+
69+
70+
@register.inclusion_tag("wiki/includes/render.html", takes_context=True)
71+
def wiki_render(context, article, preview_content=None):
72+
73+
if preview_content:
74+
content = article.render(preview_content=preview_content)
75+
elif article.current_revision:
76+
content = article.get_cached_content(user=context.get("user"))
77+
else:
78+
content = None
79+
80+
context.update(
81+
{
82+
"article": article,
83+
"content": content,
84+
"preview": preview_content is not None,
85+
"plugins": plugin_registry.get_plugins(),
86+
"STATIC_URL": django_settings.STATIC_URL,
87+
"CACHE_TIMEOUT": settings.CACHE_TIMEOUT,
88+
}
89+
)
90+
return context
91+
92+
93+
@register.inclusion_tag("wiki/includes/form.html", takes_context=True)
94+
def wiki_form(context, form_obj):
95+
~~ if not isinstance(form_obj, BaseForm):
96+
raise TypeError(
97+
"Error including form, it's not a form, it's a %s" % type(form_obj)
98+
)
99+
context.update({"form": form_obj})
100+
return context
101+
102+
103+
@register.inclusion_tag("wiki/includes/messages.html", takes_context=True)
104+
def wiki_messages(context):
105+
106+
messages = context.get("messages", [])
107+
for message in messages:
108+
message.css_class = settings.MESSAGE_TAG_CSS_CLASS[message.level]
109+
context.update({"messages": messages})
110+
return context
111+
112+
113+
@register.filter
114+
def get_content_snippet(content, keyword, max_words=30):
115+
116+
def clean_text(content):
117+
118+
content = striptags(content)
119+
words = content.split()
120+
121+
122+
## ... source file continues with no further BaseForm examples...
123+
124+
```
125+
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
title: django.forms CheckboxInput Example Code
2+
category: page
3+
slug: django-forms-checkboxinput-examples
4+
sortorder: 500011258
5+
toc: False
6+
sidebartitle: django.forms CheckboxInput
7+
meta: Python example code for the CheckboxInput class from the django.forms module of the Django project.
8+
9+
10+
CheckboxInput is a class within the django.forms module of the Django project.
11+
12+
13+
## Example 1 from django-jet
14+
[django-jet](https://github.com/geex-arts/django-jet)
15+
([project documentation](https://jet.readthedocs.io/en/latest/),
16+
[PyPI project page](https://pypi.org/project/django-jet/) and
17+
[more information](http://jet.geex-arts.com/))
18+
is a fancy [Django](/django.html) Admin panel replacement.
19+
20+
The django-jet project is open source under the
21+
[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
22+
23+
[**django-jet / jet / templatetags / jet_tags.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/templatetags/jet_tags.py)
24+
25+
```python
26+
# jet_tags.py
27+
from __future__ import unicode_literals
28+
import json
29+
import os
30+
from django import template
31+
try:
32+
from django.core.urlresolvers import reverse
33+
except ImportError: # Django 1.11
34+
from django.urls import reverse
35+
36+
~~from django.forms import CheckboxInput, ModelChoiceField, Select, ModelMultipleChoiceField, SelectMultiple
37+
from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
38+
from django.utils.formats import get_format
39+
from django.utils.safestring import mark_safe
40+
from django.utils.encoding import smart_text
41+
from jet import settings, VERSION
42+
from jet.models import Bookmark
43+
from jet.utils import get_model_instance_label, get_model_queryset, get_possible_language_codes, \
44+
get_admin_site, get_menu_items
45+
46+
try:
47+
from urllib.parse import parse_qsl
48+
except ImportError:
49+
from urlparse import parse_qsl
50+
51+
52+
register = template.Library()
53+
assignment_tag = register.assignment_tag if hasattr(register, 'assignment_tag') else register.simple_tag
54+
55+
56+
@assignment_tag
57+
def jet_get_date_format():
58+
return get_format('DATE_INPUT_FORMATS')[0]
59+
60+
61+
@assignment_tag
62+
def jet_get_time_format():
63+
return get_format('TIME_INPUT_FORMATS')[0]
64+
65+
66+
@assignment_tag
67+
def jet_get_datetime_format():
68+
return get_format('DATETIME_INPUT_FORMATS')[0]
69+
70+
71+
@assignment_tag(takes_context=True)
72+
def jet_get_menu(context):
73+
return get_menu_items(context)
74+
75+
76+
@assignment_tag
77+
def jet_get_bookmarks(user):
78+
if user is None:
79+
return None
80+
return Bookmark.objects.filter(user=user.pk)
81+
82+
83+
@register.filter
84+
def jet_is_checkbox(field):
85+
~~ return field.field.widget.__class__.__name__ == CheckboxInput().__class__.__name__
86+
87+
88+
@register.filter
89+
def jet_select2_lookups(field):
90+
if hasattr(field, 'field') and \
91+
(isinstance(field.field, ModelChoiceField) or isinstance(field.field, ModelMultipleChoiceField)):
92+
qs = field.field.queryset
93+
model = qs.model
94+
95+
if getattr(model, 'autocomplete_search_fields', None) and getattr(field.field, 'autocomplete', True):
96+
choices = []
97+
app_label = model._meta.app_label
98+
model_name = model._meta.object_name
99+
100+
attrs = {
101+
'class': 'ajax',
102+
'data-app-label': app_label,
103+
'data-model': model_name,
104+
'data-ajax--url': reverse('jet:model_lookup')
105+
}
106+
107+
initial_value = field.value()
108+
109+
if hasattr(field, 'field') and isinstance(field.field, ModelMultipleChoiceField):
110+
111+
112+
## ... source file continues with no further CheckboxInput examples...
113+
114+
```
115+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
title: django.forms CheckboxSelectMultiple Example Code
2+
category: page
3+
slug: django-forms-checkboxselectmultiple-examples
4+
sortorder: 500011259
5+
toc: False
6+
sidebartitle: django.forms CheckboxSelectMultiple
7+
meta: Python example code for the CheckboxSelectMultiple class from the django.forms module of the Django project.
8+
9+
10+
CheckboxSelectMultiple is a class within the django.forms module of the Django project.
11+
12+
13+
## Example 1 from dccnsys
14+
[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
15+
system built with [Django](/django.html). The code is open source under the
16+
[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
17+
18+
[**dccnsys / wwwdccn / gears / widgets.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/gears/widgets.py)
19+
20+
```python
21+
# widgets.py
22+
~~from django.forms import FileInput, CheckboxSelectMultiple, Select
23+
24+
25+
class CustomFileInput(FileInput):
26+
template_name = 'gears/widgets/file_input.html'
27+
accept = ''
28+
show_file_name = True
29+
30+
31+
~~class CustomCheckboxSelectMultiple(CheckboxSelectMultiple):
32+
template_name = 'gears/widgets/checkbox_multiple_select.html'
33+
hide_label = False
34+
hide_apply_btn = False
35+
36+
class Media:
37+
js = ('gears/js/checkbox_multiple_select.js',)
38+
39+
def __init__(self, *args, **kwargs):
40+
self.hide_label = kwargs.pop('hide_label', False)
41+
self.hide_apply_btn = kwargs.pop('hide_apply_btn', False)
42+
super().__init__(*args, **kwargs)
43+
44+
def get_context(self, name, value, attrs):
45+
context = super().get_context(name, value, attrs)
46+
context['widget'].update({
47+
'hide_label': self.hide_label,
48+
'hide_apply_btn': self.hide_apply_btn,
49+
})
50+
return context
51+
52+
53+
class DropdownSelectSubmit(Select):
54+
template_name = 'gears/widgets/dropdown_select_submit.html'
55+
empty_label = 'Not selected'
56+
57+
58+
## ... source file continues with no further CheckboxSelectMultiple examples...
59+
60+
```
61+

0 commit comments

Comments
 (0)