|
| 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 | + |
0 commit comments