django-forms

Django CSRF verification failed. Request aborted.- CSRF cookie not set

守給你的承諾、 提交于 2019-12-18 16:58:07
问题 I know this question has been asked before. I have tried almost all the options given by people but I cannot seem to solve it. I am a complete newbie so please let me know where I am going wrong. I am trying to write a simple raw form. I have not implemented any authentication or session mechanism until now (but from what I have read that does not matter to this problem. Correct me if I am wrong). When I try to submit my form, I get this error: Forbidden (403) CSRF verification failed.

Debugging form validation in Pycharm

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 16:55:15
问题 I am trying to debug form validation in Django with Pycharm. Validation of my form fails at an image I upload via an ImageField and I want to find out why it's failing on it. However, whenever I try to debug the validation process and step into and through the form initialization with the POST data, it doesn't even try validating and at the end throws an error because of empty fields when trying to save the form data to the database. It is driving me nuts... how can the behavior change

How can I modify a widget's attributes in a ModelForm's __init__() method?

天大地大妈咪最大 提交于 2019-12-18 15:16:21
问题 I want to programatically modify the widget attributes of a field in a Django ModelForm's init () method. Thus far, I've tried the following def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['my_checkbox'].widget_attrs(forms.CheckboxInput(attrs={'onclick':'return false;'})) Unfortunately, this does not work. Any thoughts? 回答1: def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['my_checkbox'].widget.attrs[

How to process two forms in one view?

可紊 提交于 2019-12-18 13:31:27
问题 I have two completely different forms in one template. How to process them in one view? How can I distinguish which of the forms was submitted? How can I use prefix to acomplish that? Or maybe it's better to write separate views? regards chriss 回答1: Personally, I'd use one view to handle each form's POST. On the other hand, you could use a hidden input element that indicate which form was used <form action="/blog/" method="POST"> {{ blog_form.as_p }} <input type="hidden" name="form-type"

django - How to cross check ModelAdmin and its inlines?

。_饼干妹妹 提交于 2019-12-18 12:43:14
问题 I have two models (ModelParent and ModelChild) with same m2m fields on Subject model. ModelChild has a foreign key on ModelParent and ModelChild is defined as inline for ModelParent on admin page. ### models.py ### class Subject(Models.Model): pass class ModelParent(models.Model): subjects_parent = ManyToManyField(Subject) class ModelChild(models.Model): parent = ForeignKey(ModelParent) subjects_child = ManyToManyField(Subject) ### admin.py ### class ModelChildInline(admin.TabularInline):

django : using admin datepicker

泪湿孤枕 提交于 2019-12-18 11:59:10
问题 I'm trying to use the admin datepicker in my own django forms. Roughly following the discussion here : http://www.mail-archive.com/django-users@googlegroups.com/msg72138.html I've a) In my forms.py included the line from django.contrib.admin import widgets b) and used the widget like this : date = forms.DateTimeField(widget=widgets.AdminDateWidget()) c) And in my actual template I've added : {{form.media}} To include the js / styles etc. However, when I try to view my form I get no nice

How do I set default widget attributes for a Django ModelForm?

跟風遠走 提交于 2019-12-18 11:55:46
问题 I'd like to set the class attribute for the TextInput Widget to one value for all the fields in my form that use it without having to list them all in Meta: widgets = {.... Is this possible? Thanks. 回答1: You can override the constructor in order to modify all TextInput widgets: from django import forms class MyForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) for name, field in self.fields.items(): if field.widget.__class__ == forms

Modify value of a Django form field during clean()

孤人 提交于 2019-12-18 11:45:15
问题 I am adding custom validation to my forms and custom fields in my Django app. I would like to be able to modify the value of a field when triggering an error. For example, if there is an error, the form should be redisplayed with the field value corrected by clean() and an error message "Data has been corrected below. Click save again to confirm if these changes are OK" I've tried returning the modified data in cleaned_data[] like this but it doesn't work. It displays the error correctly, but

How to debug a Django MultiValueDictKeyError on Formset POST

大憨熊 提交于 2019-12-18 11:31:18
问题 When I post my formset, I get a MultiValueDictKeyError. Specifically: MultiValueDictKeyError at /core/customers/1/update/documents/ "Key u'documents-0-attachment_ptr' not found in <QueryDict: {u'documents-1-last_modified_date': [u''], u'documents-1-name': [u''], u'documents-MAX_NUM_FORMS': [u''], u'documents-0-attachment_file': [u''], u'documents-INITIAL_FORMS': [u'1'], u'documents-1-document_type': [u''], u'documents-0-notes': [u''], u'documents-1-notes': [u''], u'submit': [u'Submit changes'

Populate a django form with data from database in view

时光毁灭记忆、已成空白 提交于 2019-12-18 11:07:52
问题 I have a form in my forms.py that looks like this: from django import forms class ItemList(forms.Form): item_list = forms.ChoiceField() I need to populate the item_list with some data from the database. When generated in HTML item_list should be something like: <select title="ItemList"> <option value="1">Select Item 1</option> <option value="2">Select Item 2</option> </select> The options values in my select statement will change almost every time since a variable in the query will often