django-forms

jQuery autocomplete with many inputs - django-dynamic-formset

浪尽此生 提交于 2019-12-23 03:12:43
问题 I'm using jQuery autocomplete feature (http://jqueryui.com/autocomplete) and Django dynamic-formset (http://code.google.com/p/django-dynamic-formset/). One of my formset fields needs to use autocomplete, so I attach it using (simplified): $('input[name$=select]').autocomplete({source:'my_url/', minLength: 3}); I call this on document ready and it works fine (gets data from ajax). However, if I add new row with django-dynamic-formset and handle its 'added' event with: function(row){ $(row)

Use validation to prevent duplicate file _name_ being uploaded

♀尐吖头ヾ 提交于 2019-12-23 02:57:20
问题 How can I detect that the name of a file that a user has provided for upload (via a django.forms.ModelForm using a FileField field) is a duplicate of one that exists, and thus decide to fail validation on the form? I'm finding this particularly challenging, because from within the form, I don't see how I can find out what the value of upload_to is for this FileField , so I can't go looking myself in the file system to see if that file is there already. 回答1: As i see it you have 2 options: Set

Django - 403 Forbidden. CSRF token missing or incorrect

痞子三分冷 提交于 2019-12-23 02:39:39
问题 I try to add ModelForm for my model, but every POST attempt ends with "403 Forbidden. CSRF verification failed. Request aborted. Reason given for failure: CSRF token missing or incorrect". I have no render_to_response() method, so I can't fix this problem by adding RequestContext. Here's my model: from django.db import models from django.forms import ModelForm . . . class Text(models.Model): title = models.CharField(max_length=200) content = models.TextField() def __str__(self): return self

UNIQUE constraint failed username error while changing user details

情到浓时终转凉″ 提交于 2019-12-23 02:04:18
问题 I have custom user model by extending AbstractUser class. I want to make form to change user's fullname and website field. My user: class Hacker(AbstractUser): name = models.CharField(max_length=255) team = models.ForeignKey(Team, on_delete=models.CASCADE, blank=True, null=True) description = models.CharField(max_length=255, blank=True, null=True) website = models.URLField(max_length=200, blank=True, null=True) def __str__(self): if self.name: return self.name else: return self.username And

What's the difference between these two ways to override the save() method in a Django ModelForm?

ぃ、小莉子 提交于 2019-12-23 00:26:53
问题 I've come across two methods of doing this. The accepted answer here suggests: def save(self, *args, **kwargs): instance = super(ModelClass, self).save(commit=False) instance.my_stuff = manual_value instance.save() But the following, found here, seems more elegant: def save(self, *args, **kwargs): self.my_stuff = manual_value super(ModelClass, self).save(*args, **kwargs) Is there any reason to choose one over the other, other than the latter being one less line, such as a reason for running

Python - Update a user in Django 2.2

烈酒焚心 提交于 2019-12-23 00:26:13
问题 I'm working on a project using Python(3.7) and Django(2.2) in which I have implemented models for multiple types of users by extending the base User model. Now I need to implement a way to allow the admin user to edit a user ( can't use the default Django admin). So, I have utilized the MultiModelForm to combine multiple forms on a single template, on the get request the form is loading properly with data populated. Here's what I have done so far: From models.py : class User(AbstractBaseUser,

How to test a Django form with a ModelChoiceField using test client and post method

六眼飞鱼酱① 提交于 2019-12-22 18:29:20
问题 How do I use Django test client.post to test a form that has a ModelChoiceField? How should the data dictionary passed to the post method be written? The way I am doing does not select any value at all. I have a form with the following field: country = forms.ModelChoiceField( label="País", queryset=Country.objects.all().order_by('name'), required=True, widget=forms.Select(attrs={ 'onchange': "Dajaxice.party.update_country(Dajax.process, {'option':this.value})" }, ) I also have the following

How to test a Django form with a ModelChoiceField using test client and post method

心已入冬 提交于 2019-12-22 18:29:06
问题 How do I use Django test client.post to test a form that has a ModelChoiceField? How should the data dictionary passed to the post method be written? The way I am doing does not select any value at all. I have a form with the following field: country = forms.ModelChoiceField( label="País", queryset=Country.objects.all().order_by('name'), required=True, widget=forms.Select(attrs={ 'onchange': "Dajaxice.party.update_country(Dajax.process, {'option':this.value})" }, ) I also have the following

Django: Display Generic ModelForm or predefined form

安稳与你 提交于 2019-12-22 18:19:52
问题 I have 3 models with various fields in each. For 2 of the models, I'm fine with using a generic form (through Django's create_object) to request data. I wrote a function that accepts the model name and sends the user to the generic form url(r'^add_(?P<modelname>\w+)/$', generic_add), def generic_add(request, modelname): mdlnm_model = models.get_model('catalog',modelname) return create_object(request, model = mdlnm_model, template_name = 'create.html', post_save_redirect = '/library/', extra

Django ChoiceField get currently selected

社会主义新天地 提交于 2019-12-22 18:12:16
问题 I have a simple ChoiceField and want to access the 'selected' item during my template rendering. Lets say the form gets show again (due to error in one of the fields), is there a way to do something like: <h1> The options you selected before was # {{ MyForm.delivery_method.selected }} </h1> (.selected() is not working..) Thanks ! 回答1: It would be accessed by {{ myform.delivery_method.data }} <h1> The options you selected before was # {{ MyForm.delivery_method.data }} </h1> 回答2: @Yuji