I want to display a field (named 'icon') as radio button.
I created method callback in order to display DateTimeFields with JQuery. The code following should do it, however i get this error when i run my server:
Error when calling the metaclass bases
make_custom_datefield() got an unexpected keyword argument 'widget'
...
Exception Location: Virtualenvs/django/local/lib/python2.7/site-packages/django/forms/models.py in fields_for_model, line 164
forms.py:
def make_custom_datefield(f):
formfield = f.formfield()
if isinstance(f, DateTimeField):
formfield.widget.format = '%m/%d/%Y'
formfield.widget.attrs.update({'class':'datetimePicker', 'readonly':'true'})
return formfield
class FlashForm(forms.ModelForm):
formfield_callback = make_custom_datefield
class Meta:
model = Flash
exclude=('user','marker','address')
widgets = {'icon': forms.RadioSelect(), }
Can you please help me, i have really no clue how to solve this !
Thanks
Eventually, i found the answer: i had to add **kwargs parameter.
def make_custom_datefield(f,**kwargs):
formfield = f.formfield(**kwargs)
if isinstance(f, DateTimeField):
formfield.widget.format = '%m/%d/%Y'
formfield.widget.attrs.update({'class':'datetimePicker', 'readonly':'true'})
return formfield
Related
I have simple flask user validation form in wtform:
class LoginForm(FlaskForm):
email = StringField('Email', validators=[validators.DataRequired(), Email()])
password = PasswordField('Password', validators=[validators.DataRequired(), validators.Length(min=8, max=20)])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Sign In')
In my app.py file:
#app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and bcrypt.check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
return redirect(next_page) if next_page else redirect(url_for('home'))
else:
flash('Login Unsuccessful. Please check email and password', 'danger')
return render_template('prihl.html', title='Login', form=form)
Everything what should be is imported.
I receive always Type error:
TypeError: LoginForm.validate() got an unexpected keyword argument 'extra_validators'
And problem is referenced to the file
"C:\Users*******.virtualenvs\flaskGPT-yypoX3qF\lib\site-packages\flask_wtf\form.py", line 86, in validate_on_submit
Is there someone who could help me with this ?
I have tried to authenticate user. Connection to the db is correct. I have migrated models succesfully. The problem is only with loginform, not with registerform.
Is it possible to pass parameters from URL to a view function decorated with #api_view or I need to use APIView class instead?
Yes it's possible. What you've to do is access the request.query_params as below,
#api_view()
def sample_view(request, kw, *args,**kwargs):
url_params = request.query_params
# ypur code
can somebody tell me what's wrong with this code? This is located in my controller. I have an error message "Message: Call to undefined method CI_Input::manufacturer()"
public function edit_manufacturer(){
$this->load->helper("security");
$id = $this->uri->segment(3);
if($this->input->manufacturer('submit')){
$manufacturer_name = $this->security->xss_clean($this->input->manufacturer('manufacturer_name'));
$this->asset_model->edit_manufacturer($manufacturer_id, $manufacturer_name);
}
}
input is a reserved class from CodeIgniter and the methods are the following
$this->input->post();
$this->input->get();
$this->input->cookie();
$this->input->server();
therefore, manufacturer method doesn't exist unless you modified this class and created the method.
Maybe what you want to do is:
$this->input->post('manufacturer');
or
$this->input->post('submit');
For more information visit Input Class Documentation
I have a controller action with something like:
#widget = Widget.new(permitted_params)
#widget.user_id = current_user.id
if #widget.save
#widget
else
{ errors: #widget.errors.full_messages }
end
And I'm trying to create a spec for that controller.
widget = mock_model(Widget)
allow(Widget).to receive(:new).and_return(widget)
allow(widget).to receive(:user_id).and_return(widget)
allow(widget).to receive(:save).and_return(true)
expect(widgets).to receive(:build)
expect(widget).to receive(:save)
post '/v2/widgets', name: 'foo'
expect(json_response).to eq widget.as_json
Now the weird thing that I'm getting :
Failure/Error: post '/v2/widgets', name: 'foo'
#<Double "Widget_1133"> received unexpected message :user_id= with (1129)
Even when I have
allow(widget).to receive(:user_id).and_return(widget)
Any help what is the mistake i'm doing?
Thanks
Ok got it.
Seems I was supposed to use:
allow(widget).to receive(:user_id=).and_return(widget)
:user_id=, not :user_id
I am trying to test and AJAX view in my Django Project. When submit the post from JQuery the data is correctly accessible in the Django View but when I try to make queries for the Django test client it is emplty.
Here is the code I use:
The view
def add_item(request):
if request.is_ajax() and request.method == 'POST':
post_data = request.POST
print post_data ## <----- THIS IS EMPTY
name = post_data.get('name')
# Render the succes response
json_data = json.dumps({"success":1})
return HttpResponse(json_data, content_type="application/json")
else:
raise Http404
And the test
class TestAddItem(TestCase):
def test_some_test(self):
data = {
'description':"description",
}
response = self.client.post('theurl', data, content_type='application/json')
Any Idea what I might be doing wrong?
I tried also without content type and also using plain url like thurl/?name=name without succes.
Any help will be appreciated.
Olivier
After trying different combinations of parameter formating, content types, etc..
I found one solution that works :
response = self.client.post(reverse('theurl'), data,
**{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'})
And I get the following dictionary on the POST parameters of the request:
<QueryDict: {u'name': [u'name']}>
Edit I love testing :)