'tuple' object has no attribute 'fields' using Django Crispy Forms - django-crispy-forms

Given a form, I want to wrap some fields in Div using django-crispy-forms.
class SignupForm(forms.Form):
def __init__(self, *args, **kwargs):
super(SignupForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'POST'
self.helper.form_action = ''
self.helper.add_input(Submit('submit', 'Create Account', css_class="btn-success"))
self.helper.layout = Layout(
Div(
'username',
'password',
css_class="col-md-6"
),
Div(
'name',
'age',
css_class="col-md-4"
),
),
username=forms.CharField(max_length=128)
password=forms.CharField(max_length=128, widget=forms.PasswordInput())
name=forms.CharField(max_length=128)
age=forms.IntegerField(required=False)
However, in the view, when I use {% crispy form %}, it is throwing me the error 'tuple' object has no attribute 'fields'.
I'm suspecting that my Layout was not instantiated correctly but cannot move from here.

Solved it myself. The solution to this was that there was an extra comma at the end of Layout. The correct solution is:
self.helper.layout = Layout(
Div(
'username',
'password',
css_class="col-md-6"
),
Div(
'name',
'age',
css_class="col-md-4"
),
)
By removing the comma a the last line.

Related

Feature image field is not showing in dashboard for custom post type

I want my result like this
enter image description here
I think 'supports' parameter missed in your custom post type. Just add following code where you registered your custom post type.
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),

'NoneType' object is not callable django

#admin.site.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish', 'status')
list_filter = ('status', 'created', 'publish', 'author')
Please help me out because i dont know what is the problem

get_extra_kwargs methon in ModelSerializer has no influence for read_only parameter of nested serializer

I'm trying to allow manipulate with nested object with serializer only when object is created. But With following code nested input and output fields are still appear in html form (but without nested fields) and throw "error": "{'output': ['This field is required.'], 'input': ['This field is required.']}" when I try to update the only adjustable status field
class OrderSerializer(serializers.ModelSerializer):
input = InputSerializer()
output = OutputSerializer()
def get_extra_kwargs(self):
extra_kwargs = super().get_extra_kwargs()
_request = self.context['view'].request
all_fields = (
'id',
'input',
'output',
'status',
)
adjustable_fields = []
if _request.method == 'PUT':
_rs = _request.parser_context['kwargs'].get('random_string')
if _rs:
adjustable_fields = [
'status',
]
elif _request.user.is_staff:
adjustable_fields = [
'input',
'status',
'custom_rate',
'custom_min_profit',
'custom_service_fee_percent'
]
else:
adjustable_fields = []
extra_kwargs.update({x: {'read_only': True} for x in all_fields if x not in adjustable_fields})
return extra_kwargs
UPD
I'm not logged in in the sample

Drupal 7 Form Api AJAX callback on a text_format field do not work

I have a field in my custom module
$form['update']['text'] = array(
//'#type' => 'textarea',
'#type' => 'text_format',
If I use textarea everything is ok, but if I use the text_format my ajax callback do not change the value of the field.
function maintenance_update_form_update_callback($form, $form_state) {
$entry = $form_state['entries'][$form_state['values']['aid']];
// Setting the #value of items is the only way I was able to figure out
// to get replaced defaults on these items. #default_value will not do it
// and shouldn't.
dpm($form_state['entries'][$form_state['values']['aid']]);
foreach (array('status', 'type', 'title', 'text', 'name', 'email', 'phone', 'notes') as $item) {
$form['update'][$item]['#value'] = $entry->$item;
//dpm($entry);
//drupal_set_message("entry->item értéke: ENTRY: $entry, ITEM: $item , $entry->$item");
}
What can be wrong with this field? As far as I know this type also support ajax requests....
//drupal_set_message('Callback $form');
dpm($form);
return $form;
}
It's very simple - text_format is not simple single-value form item. It takes array for value param like that
array(
'value' => '123123',
'format' => 'html'
)
So, if you want to change value try this code:
$form['update']['text']['value']['#value'] = 'text message';
Adding to #ProFak solution, if you want to change the format of the text_format field type, you will have to use the following code
$altered_text_format = 'full_html';
$form['update']['format']['format']['#value'] = $altered_text_format;
So the complete solutions will look like as below
function maintenance_update_form_update_callback($form, $form_state) {
$entry = $form_state['entries'][$form_state['values']['aid']];
// Setting the #value of items is the only way I was able to figure out
// to get replaced defaults on these items. #default_value will not do it
// and shouldn't.
foreach (array('status', 'type', 'title', 'text', 'name', 'email', 'phone', 'notes') as $item) {
// #ProFak Solution
$form['update']['text']['value']['#value'] = $entry->$item;
// #Sukhjinder Singh additional solution
$altered_text_format = 'full_html';
$form['update']['format']['format']['#value'] = $altered_text_format;
}
return $form['update'];
}

Yii. CGridView in frontend. CActiveDataProvider without $model->search()

I don't know if it is possible, but suppose it must be.
I need to present some data in frontend view - the list of companies in tabular form:
Company name 1 - Country 1 - Website 1 - etc...
Company name 2 - Country 2 - Website 2 - etc...
etc...
I'm trying to use CGridView for this purpose.
My controller:
public function actionList()
{
$sort = new CSort();
$sort->attributes = array(
'defaultOrder'=>'company_name DESC',
'company_name'=>array(
'asc'=>'company_name ASC',
'desc'=>'company_name DESC',
),
'country'=>array(
'asc'=>'country ASC',
'desc'=>'country DESC',
),
);
$criteria = new CDbCriteria();
$criteria->order = "company_name DESC";
$criteria->condition = 'approve = :approve';
$criteria->params = array(':approve'=>1);
$dataProvider = new CActiveDataProvider('EuCompanies', array('criteria'=>$criteria,'sort'=>$sort));
$this->render('list',
array('dataProvider'=>$dataProvider,
));
}
My view:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns'=>array(
'company_name',
'country',
'company_website',
'economic_sector',
'contact_person',
'email',
'phone_number'
),
));
?>
The output is a nice grid, which takes into account the selection criteria, and clickable "Company name" and "Country", but clicking on them doesn't make the column sort. I guess it is because sort can't be done without using $model->search() in dataProvider, or I've just done something wrong?
Ok, solved. Two points were wrong:
First:
$criteria->order = "company_name DESC";
was over-riding any other sort. After deleting this string, my columns become sortable.
Second (default sort didn't work):
Had to move
$sort->attributes = array(
'defaultOrder'=>'company_name DESC',
from attributes to
$sort = new CSort();
$sort->defaultOrder = 'company_name ASC';
$sort->attributes = array(...
Work done, thank you :)

Resources