django-forms

Django - change field validation message

南楼画角 提交于 2019-12-30 18:05:44
问题 I have an email field in my Newsletter form that looks like this: class NewsletterForm(forms.ModelForm): email = forms.EmailField(widget=forms.EmailInput(attrs={ 'autocomplete': 'off', 'class': 'form-control', 'placeholder': _('seuemail@email.com'), 'required': 'required' })) class Meta: model = Newsletter fields = ['email', ] My form is working, but when I type "ahasudah@ahs" without a DOT for the domain name, I get this error "Enter a valid email address" Where is this? I just checked the

Filter select field in ModelForm by currently logged in user

╄→尐↘猪︶ㄣ 提交于 2019-12-30 13:36:21
问题 I'm trying to display a form (ModelForm) with a select field filtered by currently logged in user. The select field in this case contains a list of categories. I want to display only the categories which "belong" to the currently logged in user. The category field is a foreign key to the IngredienceCategory model. Here is what I've come up with so far but it's giving me an error (unexpected keyword queryset). Any ideas what I'm doing wrong? # models.py class IngredienceCategory(models.Model):

Django raising 404 with a message

此生再无相见时 提交于 2019-12-30 08:41:45
问题 I like to raise 404 with some error message at different places in the script eg: Http404("some error msg: %s" %msg) So, in my urls.py I included: handler404 = Custom404.as_view() Can anyone please tell me how should I be handling the error in my views. I'm fairly new to Django, so an example would help a lot. Many thanks in advance. 回答1: In general, 404 error is "page not found" error - it should not have customizable messages, simply because it should be raised only when a page is not found

Django raising 404 with a message

北城以北 提交于 2019-12-30 08:41:30
问题 I like to raise 404 with some error message at different places in the script eg: Http404("some error msg: %s" %msg) So, in my urls.py I included: handler404 = Custom404.as_view() Can anyone please tell me how should I be handling the error in my views. I'm fairly new to Django, so an example would help a lot. Many thanks in advance. 回答1: In general, 404 error is "page not found" error - it should not have customizable messages, simply because it should be raised only when a page is not found

Django form validation: making “required” conditional?

不想你离开。 提交于 2019-12-30 08:08:04
问题 I'm new to Django (and Python), and am trying to figure out how to conditionalize certain aspects of form validation. In this case, there's a HTML interface to the application where the user can choose a date and a time from widgets. The clean method on the form object takes the values of the time and date fields and turns them back into a datetime . In addition to the HTML interface, there's also an iPhone client making calls into the application, and I'd like to pass a UNIX timestamp-style

Django - Show BooleanField in a formset as one group of radio buttons

為{幸葍}努か 提交于 2019-12-30 06:37:43
问题 I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) def primary_phone(self): return self.phone_set.get(primary=True) class Phone(models.Model): profile = models.ForeignKey(Profile) type = models.CharField(choices=PHONE_TYPES, max_length=16) number = models.CharField(max_length=32) primary = models.BooleanField(default=False) def save(self, force_insert=False, force_update=False, using=None): if self.primary: # clear the primary attribute of

How insert 2 different forms on the same page in Django

北战南征 提交于 2019-12-30 05:11:06
问题 I have to insert 2 forms in the same page: 1) Registration form 2) Login form . So if I use this in the views.py: if request.method == 'POST': form = registrationForm(request.POST) if form.is_valid(): form.save() return render_to_response('template.html', { 'form': form, }) I will get error by submitting one of two forms. How can I distinguish the 2 forms submitting in the views ? 回答1: You can also do like this, <form method='POST'> {{form1.as_p}} <button type="submit" name="btnform1">Save

How to override django 'unique' error message for username in custom UserChangeForm

旧时模样 提交于 2019-12-30 04:48:26
问题 I'm trying to override the default "A user with that Username already exists." error message displayed when entering an existing username in my custom UserChangeForm form. Django version used: 1.6.1 Here's my code : class CustomUserChangeForm(forms.ModelForm): username = forms.RegexField( label="User name", max_length=30, regex=r"^[\w.@+-]+$", error_messages={ 'invalid': ("My message for invalid"), 'unique': ("My message for unique") # <- THIS } ) class Meta: model = get_user_model() fields =

Django Forms, having multiple “Models” in Meta class?

落花浮王杯 提交于 2019-12-30 04:44:04
问题 Can we define multiple models in the "Meta class" part of a Form ? Here is my example: from django import forms from django.contrib.auth.models import User , Group from django.forms import ModelForm from django.utils.translation import ugettext as _ from profiles.models import Student , Tutor class RegistrationForm(ModelForm): email = forms.EmailField(label=_('Email Address:')) password = form.CharField(label=_('Passsword:') , widget = forms.PasswordInput(render_value = False)) password1 =

Django: Passing value from template to view

荒凉一梦 提交于 2019-12-30 03:34:08
问题 I HAD this situation: Clicking on a html submit button, I call views.stream_response which "activates" views.stream_response_generator which "activates" stream.py and return a StreamingHttpResponse and I see a progressive number every second up to m at /stream_response/ : 1 2 3 4 5 6 7 8 //e.g. my default max value for m stream.py from django.template import Context, Template import time def streamx(m): lista = [] x=0 while len(lista) < m: x = x + 1 time.sleep(1) lista.append(x) yield "<div>