django-forms

Django: create HTML input array using a django form

I am trying to automate the creation of something like this: <input type='text' name='asdf[]' /> <input type='text' name='asdf[]' /> <input type='text' name='asdf[]' /> By cycling through a range in the form. I've been trying things like this, along with several other variations: # in a model class for i in range(1, prim+1): self...

django forms MultipleChoiceField reverts to original value on save

Hi, I have wrote a custom MultipleChoiceField. I have everything working ok but when I submit the form the selected values go back to the original choices even though the form validates ok. my code looks something like this: class ProgrammeField(forms.MultipleChoiceField): widget = widgets.SelectMultiple class ProgrammeForm(forms...

Django Form field initial value on failed validation

Hi, how do I set the value of a field element after a form has been submitted but has failed validation? e.g. if form.is_valid(): form.save() else: form.data['my_field'] = 'some different data' I don't really want to put it in the view though and would rather have it as part of the form class. Thanks ...

django overwrite form clean method

Hi, When overwriting a form clean method how do you know if its failed validation on any of the fields? e.g. in the form below if I overwrite the clean method how do I know if the form has failed validation on any of the fields? class PersonForm(forms.Form): title = Forms.CharField(max_length=100) first_name = Forms.CharField(...

replace values in form.data when form fails validation

Hi I have a form field which requires a json object as its value when it is rendered. When the form is submitted it returns a comma seperated string of ids as its value (not a json string). however if the form does not validate i want to turn this string of ids back into a json string so it will display properly (is uses jquery to rende...

In django 1.1, how do I find a form field's max_length attribute

I have a form that looks like this: class SampleForm(forms.Form): text = forms.CharField(max_length=100) In my django template, I want to reference the fact that the length is 100. I tried {{ form.text.max_length }} to no avail. On a related note, how do I reference this value in python? ...

How do I create an empty Django formset using modelformset_factory?

I'm creating a formset, but it seems to populate it with all of the existing data in the table for that object. I can't figure out how to start with a blank formset; the only way seems to be to delete all of the data from the table, but clearly this isn't an option. I will post code if necessary (but there's lots of it, so knowing what ...

Django Forms, Foreign Key and Initial return all associated values

I a working with Django forms. The issue I have is that Foreign Key fields and those using initial take all associated entries (all records associated with that record other then the one entry i wanted e.g instead of getting a primary key, i get the primary key, post subject, post body and all other values attributed with that record). T...

Django: Adding inline formset rows without javascript

This post relates to this: http://stackoverflow.com/questions/520421/add-row-to-inlines-dynamically-in-django-admin Is there a way to achive adding inline formsets WITHOUT using javascript? Obviously, there would be a page-refresh involved. So, if the form had a button called 'add'... I figured I could do it like this: if request.met...

How to add default value on django save form?

I have an object Task and a form that saves it. I want to automatically asign created_by field to the currently logged in user. So, my view is this: def new_task(request, task_id=None): message = None if task_id is not None: task = Task.objects.get(pk=task_id) message = 'TaskOK' submit = 'Update' el...

Sending emails with attachment in django

I'm trying to send email with some images attached in django. Code used is this snippet : http://www.djangosnippets.org/snippets/1063/. Dunno why the attachment part returns me a core error. THe code. forms.py from django import forms from common import slugify_unique from django.conf import settings from django.core.cache import cache...

Django, loop over all form errors.

Hello At my template, I want to iterate through all form errors, including the ones that are NOT belonging to a specific field. ( which means for form.errors, it should also display for __all__ errors aswell) I have tried several versions, Ie: <div id="msg"> {% if form.errors %} <div class="error"> <p><span>ERROR</span></p> ...

Unit Testing a Django Form with a FileField

I have a form like: #forms.py from django import forms class MyForm(forms.Form): title = forms.CharField() file = forms.FileField() #tests.py from django.test import TestCase from forms import MyForm class FormTestCase(TestCase) def test_form(self): upload_file = open('path/to/file', 'r') post_dict = {'ti...

How to provide an inline model field with a queryset choices without losing field value for inline records already saved.

The code displayed below is providing the choices I need for the app field, and the choices I need for the attr field when using Admin. I am having a problem with the attr field on the inline form for already saved records. The attr selected for these saved does show in small print above the field, but not within the field itself. ...

django image upload forms

I am having problems with django forms and image uploads. I have googled, read the documentations and even questions ere, but cant figure out the issue. Here are my files my models class UserProfile(User): """user with app settings. """ DESIGNATION_CHOICES=( ('ADM', 'Administrator'), ('OFF', 'Club Official'), ('MEM'...

Storing dynamic fields in Django forms

Django's form library has a feature of form sets that allow you to process dynamically added forms. For example you would use form sets if your application has a list of bookmarks you could use form sets to process multiple forms that each represent a bookmark. What about if you want to dynamically add a field to a form? An example wo...

Process muliple forms in Django view

Hi, I have 2 forms in my Django view. How can I do a check to see which one has been submitted? Thanks ...

Django Upload form to S3 img and form validation

I'm fairly new to both Django and Python. This is my first time using forms and upload files with django. I can get the uploads and saves to the database to work fine but it fails to valid email or check if the users selected a file to upload. I've spent a lot of time reading documentation trying to figure this out. Thanks! views.py de...

[Django] Automatically Update Field when a Different Field is Changed

I have a model with a bunch of different fields like first_name, last_name, etc. I also have fields first_name_ud, last_name_ud, etc. that correspond to the last updated date for the related fields (i.e. when first_name is modified, then first_name_ud is set to the current date). Is there a way to make this happen automatically or do I...

Multi choice form field in Django

Hi! I'am developing application on app-engine-path. I would like to make form with multichoice (acceptably languages for user). Code look like this: Language settings: settings.LANGUAGES = ((u"cs", u"Čeština"), (u"en", u"English")) Form model: class UserForm(forms.ModelForm): first_name = forms.CharField(max_length=100) ...