django-forms

File upload with Django: Invalid form

こ雲淡風輕ζ 提交于 2019-12-22 09:50:43
问题 First time posting a question. I've basically copied everything I've found on here and on the django documentation site and I can't figure out what I'm doing incorrectly Here is the code from my views.py file from django import forms class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField() def upload_file(request): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): response = "Form is valid." else: response = "Failed to upload

Django Admin - RelatedObjectLookups - How Does it refresh and set the select on the parent window?

∥☆過路亽.° 提交于 2019-12-22 09:25:41
问题 I want one of my forms to work just like the admin page does so I figured I'd look in the code and see how it works. Specifically I want the user to be able to click a "+" icon next to a select list and be taken to the admin page's popup form to add a new item. When they enter a new item there, I want that new item to appear in the select box, and be selected (Just like how this feature works on the admin pages). I copied the admin js libraries into my own template, and I made my link call

django forms dateField fails validation

China☆狼群 提交于 2019-12-22 09:19:52
问题 I am trying to validate a User Profiling form in django and I can't. It seems that there is something wrong with forms.dateField(). It does not validate (ie. is_valid() return false) this is my forms dateField entry: date_of_birth = forms.DateField(label=u'date of birth', input_formats='%d/%m/%Y', required=False, widget=forms.DateInput(format = '%d/%m/%Y')) I noticed that request.POST.get('date_of_birth', '') returns the correct date (ie. the date I have typed in the html form field). I also

django Model Forms customizing fields

别来无恙 提交于 2019-12-22 08:53:11
问题 I have created a model with some classes: class Student(models.Model): name = models.CharField(max_length=40) last_name = models.CharFIeld(max_length=40) (...) and in the same models.py file at the bottom I've added a class corresponding to one of my models so i can create a form: class StudentForm(ModelForm): class Meta: model = Student How do I customize form fields created via ModelForm class ? I was reading django Documentation and I can't understand overriding the default types part. For

Django: Make all formset forms required?

妖精的绣舞 提交于 2019-12-22 08:51:14
问题 Exactly like this question, except that one got closed and accepted without a real answer. It looks like I can use a custom formset and override the clean method, but that still doesn't answer how I check that they're all filled in. What properties am I supposed to be looking at? The formset is smart enough to ignore extra forms that were not changed. *Screams* This has caused me nothing but agony. 回答1: Think I found the solution by digging through the source... class BaseVehicleFormSet

Override ChoiceField widget in Django ModelForm without re-specifying choices

落爺英雄遲暮 提交于 2019-12-22 08:37:24
问题 I am trying to override the default widget for a Django ChoiceField while preserving the choices generated in the model. Here is the relevant part of my model: class UserGroup(models.Model): icon = models.CharField(max_length=100, choices=<function call>) And of my form: class UserGroupForm(forms.ModelForm): icon = models.ChoiceField(widget=IconPicker) class Meta: model = UserGroup fields = [ 'icon', ] Overriding the ChoiceField 's widget like this clobbers the form.fields['icon'].choices

Django : ModelForm with conditions

╄→гoц情女王★ 提交于 2019-12-22 08:36:23
问题 I'm trying to create a form variable. As default Player have the level 0 and he can just change is name. Later when he is level 1, he can change is name and is avatar. When he is level 3, he can change is name, is avatar and is job. Etc... Models.py: class Player(models.Model): level = models.SmallIntegerField(default=0) name = models.CharField(max_length=50) avatar = models.URLField(default='http://default-picture.com/01.png') job = models.TextField(null=True) Fomrs.py: class ProfileForm

Django ModelForm with Select Widget - Use object.uid as default option value instead of object.id

故事扮演 提交于 2019-12-22 08:25:18
问题 I have a form inheriting from ModelForm as such: class ChildModel(ModelForm): class Meta: model = Documents fields = ('secretdocs') widgets = { 'secretdocs': Select(attrs={'class': 'select'}), } The model "secretdocs" has a uid. But when it prints out the select and option, the option values appear as such: <select class="select" id="id_secretdocs" name="secretdocs"> <option value="1">My Secret Doc</option> </select> But I want it to instead have the uid of the option: <select class="select"

How to remove the “Currently” tag and link of a FileInput widget in Django?

三世轮回 提交于 2019-12-22 05:24:19
问题 I made a ModelForm from a Model in Django, the model have an ImageField field on it. When I render the info of the form in a template for editing it, it show this: How I can remove the 'Currently' tag and link?? 回答1: The Django Admin uses AdminFileWidget to render ImageFields. AdminFileWidget merely inherits from the standard FileInput widget and adds the extra "Currently" stuff. So just use FileInput instead: from django.db import models from django import forms class MyModelAdmin(admin

Testing InlineFormset clean methods

让人想犯罪 __ 提交于 2019-12-22 04:37:37
问题 I have a Django project, with 2 models, a Structure and Bracket , the Bracket has a ForeignKey to a Structure (i.e. one-to-many, one Structure has many Brackets). I created a TabularInline for the admin site, so that there would be a table of Brackets on the Structure. I added a custom formset with some a custom clean method to do some extra validation, you can't have a Bracket that conflicts with another Bracket on the same Structure etc. The admin looks like this: class BracketInline(admin