django-forms

Django Advantage forms.Form vs forms.ModelForm

There is a question very similar to this but I wanted to ask it in a different way. I am a very customized guy, but I do like to take shortcuts at times. So here it goes. I do find these two classes very similar although one "helps" the programmer to write code faster or have less code/repeating code. Connecting Models to Forms sounds ...

Accessing request.user in Django Forms.

This is pretty covered, but I'm still stucked here. I made a change password form, as you can see: class ChangePassForm(forms.Form): old_password = forms.CharField(min_length = 3, widget = forms.PasswordInput) new_password1 = forms.CharField(min_length = 3, widget = forms.PasswordInput) new_password2 = forms.CharField(min...

Initial populating on Django Forms

I decide to learn Django Forms. For awhile now been using HTML forms because its hard for me to come terms with django forms. How could i populate initial data to django forms? Example: Consider if these models are populated. Contain data. models.py class Game(models.Model): title = models.CharField() genre = models.CharField()...

Django must be a "User" instance.

I get a Must be a User Instance error message. TRACEBACK Traceback: File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response 92. response = callback(request, *callback_args, **callback_kwargs) File "/Users/ApPeL/Sites/Django/omu2/../omu2/friends/views.py" in add 13. if form.is_v...

Python: how to use value stored in a variable to decide which class instance to initiate?

I'm building a Django site. I need to model many different product categories such as TV, laptops, women's apparel, men's shoes, etc. Since different product categories have different product attributes, each category has its own separate Model: TV, Laptop, WomensApparel, MensShoes, etc. And for each Model I created a ModelForm. Hence ...

Django 1.2: Custom form field?

Hi, I've got a dynamic form that contains either one or several MultipleChoiceFields or ChoiceFields. I want to display an instruction to the user, e.g. for ChoiceField: "Select one of the following", and for MultipleChoiceField: "Select any of the following" How can I do this? I tried subclassing each of these fields, but I couldn't ...

Django admin site: how to create a single page for global settings?

I would like to create a single page in the admin site of django where I can change some global variables of the website (title of the website, items in the navigation menu, etc). At the moment I have them coded as context processors but I would like to make them editable. Something similar to what happens in WordPress. Is this possible...

Require help in Django 'local variable 'form' referenced before assignment'

Hello, I am having problem in django. I have created a form in my app where I can take details of a client. Now I want to create a form which can allow me to edit a form. However I am having some problems when I go to /index/edit_client/1, I get this error. local variable 'form' referenced before assignment I do not know what the reas...

Validate/clean a FileField on a non-model form in Django?

I'm ultimately trying to validate a FileField by extension type. But I'm having trouble even getting the clean method for this field to pickup the POSTed value. from django.forms.forms import Form from django.forms.fields import FileField from django.forms.util import ValidationError class TestForm(Form): file = FileField(r...

Django: "reverse" many-to-many relationships on forms

The easiest example of the sort of relationship I'm talking about is that between Django's Users and Groups. The User table has a ManyToMany field as part of its definition and the Group table is the "reverse" side. A note about my constraints: I am not working with the Admin interface at all nor is that an option. Now, onto the prog...

How can I set arbitrary attributes on Django Model Fields, then access them in a ModelForm?

What Django Does Django's Model Field "blank" attribute, and the way it gets negated and turned into the Form Field "required" attribute when you create a ModelForm, is pretty cool. It allows me to set the property on the model where it belongs, but have the "required" attribute available when handling a ModelForm created from it. Havin...

Externally add form errors in Django

Hi all, I have a Django form that will add an entry to a database with a unique constraint on one of the columns. If the submission happens to attempt creation of a duplicate, I get a django.db.utls.IntegrityError stating I violated the unique constraint (thankfully). When processing Django forms, there are a number of ways to valid...

Django - Form widget with a checkbox to choose between unlimited or a textbox for a number

I am making a form in Django. The field to display is a numeric field representing some limit. It's possible for there to be no limit. Rather than force the user to enter some strange number to mean unlimited (e.g. -1), I'd like there to be a radio button, with 2 options: "Unlimited" and the second option being a text box that the user c...

Django admin site: how to include conditional fields?

Hello, I wonder if is it possible to add some conditional fields in django. Say I have a category model which has an ID, name and description fields. What I would like is to add a many-to-many field in my Product model that links it with the Category ID model... and as a helping reference show what the name of that Category would be. I k...

Handling graceful degradation within a Django form

I have a form that looks similar to the following (simplified for brevity): PRICING_PATTERN = r'(?:^\$?(?P<flat_price>\d+|\d?\.\d\d)$)|(?:^(?P<percent_off>\d+)\s*\%\s*off$)' class ItemForm(forms.Form): pricing = forms.RegexField( label='Pricing', regex=PRICING_PATTERN ) pricing_type = forms.CharField( ...

Django admin site: how to compute field from multiple fields value?

I am wondering if is there a way to compute a field in the admin site based on a concatenation of multiple fields. Basically I have a Product model with different fields associated to various attributes (colour, size, length etc). I would like to compute the code value to be a concatenation of the values of the various attribute field...

Django admin site: prevent fields from being edited?

Hello, is it possible to prevent certain fields to be edited after they've been saved? They should be editable when the user creates a new item of a certain model but then when they try to open them to edit certain fields are 'blocked'. thanks ...

Django admin site: implement a singleton for global variables?

Does anyone have experience (Django 1.x pref 1.3) with implementing a sort of singleton accessible from the admin page to expose some global variables for editing (site name, keywords, ...). I cant find anything like this and it sounds quite unbelievable! thanks (django-preferences is broken with 1.x) ...

Django: How to override form.save()?

My model has quite a few boolean fields. I've broken these up into 3 sets which I'm rendering as a MultipleChoiceField w/ a modified CheckboxSelectMultiple. Now I need to save this data back to the DB. i.e., I need to split the data returned by a single widget into multiple boolean columns. I think this is appropriate for the save() met...

How to override a field value before validation?

I've got a form for which I need to set a few values before validation. I'm trying: if request.method == 'POST': reservation_form = ReservationForm(request.POST, initial={'is_reservation':True, 'user':request.user}) But it doesn't work. If I exclude the 'user' field from the (model)form, I get a null-constraint error, if I do incl...