This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.forms.widgets import TextInput | |
from django.utils.translation import ugettext_lazy as _ | |
class TelephoneInput(TextInput): | |
# switch input type to type tel so that the numeric keyboard shows on mobile devices | |
input_type = 'tel' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.views.generic import UpdateView | |
from forms import MyModelForm | |
from models import MyModel | |
class MyUpdateView(UpdateView): | |
# specify a custom ModelForm | |
form_class = MyModelForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from app.models import User | |
from app.forms.widgets import * | |
class RegisterBaseForm(forms.ModelForm): | |
username = forms.CharField(max_length=User._meta.get_field('username').max_length) | |
email = forms.CharField(max_length=User._meta.get_field('email').max_length, widget=HTML5EmailInput()) | |
password = forms.CharField(min_length=6, max_length=16, widget=forms.PasswordInput()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
remember_me = forms.BooleanField(required=False, widget=forms.CheckboxInput()) | |
if not self.cleaned_data.get('remember_me'): | |
self.request.session.set_expiry(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ids = [9, 8, 1, 2, 7, 3] | |
results = Model.objects.filter(id__in=ids).extra( | |
select={'manual': 'FIELD(id,%s)' % ','.join(map(str, ids))}, | |
order_by=['manual'] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreditCardForm(ModelForm): | |
card_number = CreditCardField(placeholder=u'0000 0000 0000 0000', min_length=12, max_length=19) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from app.models import User | |
from django.contrib.auth.hashers import check_password | |
from django.db.models import Q | |
class SignInForm(forms.Form): | |
username = forms.CharField(max_length=User._meta.get_field('email').max_length) | |
password = forms.CharField(min_length=6, max_length=16, widget=forms.PasswordInput()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
register = template.Library() | |
@register.tag(name='captureas') | |
def do_captureas(parser, token): | |
try: | |
tag_name, args = token.contents.split(None, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load captureas %} | |
{% captureas message %}{% spaceless %}{% block message %}{% endblock %}{% endspaceless %}{% endcaptureas %} | |
{% if message %} | |
<section id="message" class="messages alert"> | |
<p>{{ message }}</p> | |
</section> | |
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.template.defaultfilters import filesizeformat | |
from django.utils.translation import ugettext_lazy as _ | |
from django.conf import settings | |
class RestrictedFileField(forms.FileField): | |
def __init__(self, *args, **kwargs): | |
self.content_types = kwargs.pop('content_types', None) | |
self.max_upload_size = kwargs.pop('max_upload_size', None) | |
if not self.max_upload_size: |
NewerOlder