django-forms

Dynamically added form fields are removed in form.cleaned_data

不打扰是莪最后的温柔 提交于 2020-01-02 03:15:14
问题 I put some client-side Javascript in my template that allows a user to dynamically add fields to a form. My problem is that these fields are cleaned in form.cleaned_data , so I can't access them that way. All the fields are accessible in request.POST , so I could just solve this problem with that, but I want to do this the "right way" and I think that the solution lies somewhere in using django forms rather than reading the request directly. I tried overriding form.clean() , but it seems like

How to use django UserCreationForm correctly

空扰寡人 提交于 2020-01-02 03:13:29
问题 I am new to Django and am just starting my first website. I am trying to set registration for new users. I used the built in view for login and logout but there is none for registration, in the doc, it says that I should use built in form : UserCreationForm. The code of my view is : def register(request): if request.method =='POST': form = UserCreationForm(request.POST) if form.is_valid(): user = User.objects.create_user(form.cleaned_data['username'], None, form.cleaned_data['password1'])

Django get instance in inline form admin

落爺英雄遲暮 提交于 2020-01-02 02:19:43
问题 Have a inline form class: class ItemColorSelectForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(ItemColorSelectForm, self).__init__(*args, **kwargs) #here i need current object Inline class: class ItemColorSelectInline(generic.GenericTabularInline): model = ColorSelect extra = 1 form = ItemColorSelectForm Admin class class ItemAdmin(admin.ModelAdmin): inlines = [ItemColorInline,] Question: how can a get current object in ItemColorSelectForm . print kwargs return: {'auto_id':

Simple form not validating

泄露秘密 提交于 2020-01-01 17:57:06
问题 I have found here on stackoverflow a method to extend django's built-in authentication using signals. My base User is defined by 'email' and passwords (so no username there). So I'm trying to modify it to my needs, but I'm geting a validation error for my form. Strange thing is that error is connected to the User.email field and I'm getting 'already in use' even though I'm just registering at the moment. Is it trying to save it 2 times or what ? I've discovered it when I was sending

Model Formset - By Default model formset is rendering one extra field (2 fields in total)

随声附和 提交于 2020-01-01 14:45:13
问题 My model formset without even defining "extra" parameters in modelformset_factory is rendering one extra field in the template. I have tried many variations but it didn't work. If I print the form (the model form) on the command line it just prints a single form field as required but on model formset it prints 2 by default. Here is my code. models.py class Direction(models.Model): text = models.TextField(blank=True, verbose_name='Direction|text') forms.py class DirectionForm(forms.ModelForm):

Django form to indicate input type

[亡魂溺海] 提交于 2020-01-01 10:15:15
问题 Another basic question I'm afraid which I'm struggling with. I've been through the various Django documentation pages and also search this site. The only thing I have found on here was back in 2013 which suggested setting up a custom filter template. Anyhow, I'm trying to generate my own form instead of using Django's own way of generating it through {{ form }}. This is simply so I can control the way the form is presented. I've worked out various ways to access the required information such

How add a 'Star *' after a django ModelForm CharField?

◇◆丶佛笑我妖孽 提交于 2020-01-01 10:13:30
问题 i have some necessary fields in my django ModelForm. How can i add a red star (*) after the required fields ? 回答1: I'm going to assume you want this to happen automatically, so here's one of a few different ways: {% for field in form %} <label for="{{ field.auto_id }}">{{ field.label_tag }} {% if field.field.required %} <span class="required">*</span> {% endif %} </label> {% endfor %} Then you can style the asterisk using CSS. Or, you can add the asterisk using CSS instead if you want: <style

How to hide a field in django modelform?

跟風遠走 提交于 2020-01-01 06:57:10
问题 For example: class TestModel(models.Model): ref1 = models.ForeignKey(RefModel) text1 = models.TextField() class TestModelForm(ModelForm): class Meta: model = TestModel fields = ('text1') I only allow the user to input text1 field, but when I redefine the post method of my view, I also want to set the ref1 value, how should I do that? I wish I can let TestModelForm has the ref1 field but don't let user modify it, then I can modify the value of request.POSt in post method, and pass it to

Can I create django model, which will be not persisted in database?

百般思念 提交于 2020-01-01 06:51:10
问题 As asked in question: Can I create django model(models.Model), which will be not persisted in database? The reason is I want use it in my django admin to build custom form basing on forms.ModelForm 回答1: You can override the model's save() method to prevent saving to the database, and use managed = False to prevent Django from creating the appropriate tables: class NonPersistantModel(models.Model): def save(self, *args, **kwargs): pass class Meta: managed = False Note that this will raise an

How to get a single widget to set 2 fields in Django?

穿精又带淫゛_ 提交于 2020-01-01 06:46:46
问题 I got a model with 2 fields: latitude and longitude. Right now they're 2 CharFields, but I want to make a custom widget to set it in admin - was thinking about displaying Google Maps, then getting the coordinates of the marker. But can I have 1 widget (a single map) to set 2 different fields? 回答1: Define lat and long fields on your form; set them to use the HiddenInputWidget. After the init of your form, add another field which contains your custom widget that takes two values. In the clean