Showing posts with label error. Show all posts
Showing posts with label error. Show all posts
Monday, July 10, 2017
Django trick keep runserver from crashing on Python syntax error
Django trick keep runserver from crashing on Python syntax error
When developing Django, the "runserver" command is convenient. It runs our appserver, and reloads itself when we change our apps source code. We can have a rapid "edit stuff then see what happened" cycle.
However, runserver it has an annoying habit. If were typing so fast that we add a Python syntax error into our code, the command will crash. We expect that when we fix the syntax error, we can see our results, but it doesnt work. The appserver has crashed, and is no more.
The following workaround works wonders. If we add a syntax error, "runserver" will crash, and this loop will wait for a moment then re-run it. We can now type as fast as possible all the time, and Django will always show us what we want to see. Or, what we give it, which sometimes is enough ;)
while true; do ./manage.py runserver; sleep 15; done
Saturday, June 10, 2017
Disable Apport Error Report Dialog in Ubuntu 12 04
Disable Apport Error Report Dialog in Ubuntu 12 04
In fresh Ubuntu 12.04 installation, it keeps popping up annoying apport error report dialog on every log-in even after sending the error report. This simple tutorial will show you how to disable this dialog in Ubuntu.

Open up terminal from the dash home or press Ctrl+Alt+T, edit �/etc/default/apport� file with this command:
sudo gedit /etc/default/apport
Set enable=0:
enable=0
Save the file, and done!
Tuesday, May 2, 2017
Django celery tasks timezone error
Django celery tasks timezone error
For some reason when I access the celerys tasks page in the Djangos admin (.../djcelery/taskstate/), I got this error:
Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?
I have to do the following to get rid of the error:
1. Uncomment these settings:
# CELERY_TIMEZONE = UTC
# CELERY_ENABLE_UTC = True
2. Add this under mysqld section in /etc/mysql/my.cnf:
default-time-zone = "+07:00"
3. Run the following command (without sudo):
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
4. Restart mysql:
$ sudo service mysql restart
Monday, April 3, 2017
django Show form validation error with AJAX and jQuery
django Show form validation error with AJAX and jQuery
I once had a situation when I submited form with AJAX and if there were validation errors I had to put them at the same place as form was normally submited.
Assume that we have edit_user_data method in the views.py. If edit_form is valid we do some really useful things. If its not, we should show user form validation errors (btw, I use uni_forms in that example).
Here is our code:
def edit_user_data(request):
# ...
if edit_form.is_valid():
# ...
else:
error_message = dict([(key, [unicode(error) for error in value]) for key, value in edit_form.errors.items()])
return HttpResponse(simplejson.dumps({"result": error_message, }), mimetype='application/json')
error_message is a dictionary which contains all form errors. We use JSON so we could have access to the data like server_response.result[key]. Key is the name of the field.
Here is our javascript code:
$("#edit_form").ajaxSubmit({
url: save_url,
type: 'post',
success: function (server_response) {
// Inform about successfull save
if (server_response.result_success) {
// ...
}
// Remove form errors
$("#edit_form").find(".errorField").remove();
for (var key in server_response.result) {
// Check for proper key in dictionary
if (key in {first_name: 1, last_name: 1, gender: 1, sex: 1, phone: 1, }) {
// Get error message
error = server_response.result[key][0];
// Find related field
field = $("#edit_form").find("#div_id_" + key)
// Attach error message before it
field.before('<p class="errorField"><em></em>' + error + '</p>');
}
}
},
});
#edit_form - id of the submited form, url - where we submit the data to. If AJAX request successfuly was proceed by server (it doesnt matter if user submited valid data or not. It means he does submited something) we now could show user if his form is valid or not. If it is not valid, we remove old errors from form (or we will have multiple errors under the fields, probably duplicates). Let also assume that our form contains 5 fields: first_name, last_name, gender, sex, phone. Here you can see nice trick how to check if a key is related to one of our fields:
if (key in {first_name: 1, second_name: 1, gender: 1, sex: 1, phone: 1, }) {}
We also want to show user only one error per field (If there are more, he will be able to see them next time):
error = server_response.result[key][0];
To show error message above the field itself we need to find that field:
field = $("#edit_form").find("#div_id_" + key)
Luckly our key names are the same as field names :) And then attach a message before it:
field.before(+ error +
);
Where error is description of error, for example, "This field is required". Done. Errors looks nice and native ")
Subscribe to:
Posts (Atom)