How to handle “matching query does not exist” when getting an object

That depends on what you want to do if it doesn’t exist.. Theres get_object_or_404: Calls get() on a given model manager, but it raises Http404 instead of the model’s DoesNotExist exception. get_object_or_404(World, ID=personID) Which is very close to the try except code you currently do. Otherwise theres get_or_create: personalProfile, created = World.objects.get_or_create(ID=personID) Although, If you … Read more

Django – show loading message during long processing

After trying the two different approaches suggested by Brandon and Murat, Brandon’s suggestion proved the most successful. Create a wrapper template that includes the javascript from http://djangosnippets.org/snippets/679/. The javascript has been modified: (i) to work without a form (ii) to hide the progress bar / display results when a ‘done’ flag is returned (iii) with … Read more

Django Passing data between views

There are different ways to pass data between views. Actually this is not much different that the problem of passing data between 2 different scripts & of course some concepts of inter-process communication come in as well. Some things that come to mind are – GET request – First request hits view1->send data to browser … Read more

How do I call a Django function on button click?

here is a pure-javascript, minimalistic approach. I use JQuery but you can use any library (or even no libraries at all). <html> <head> <title>An example</title> <script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”></script> <script> function call_counter(url, pk) { window.open(url); $.get(‘YOUR_VIEW_HERE/’+pk+”https://stackoverflow.com/”, function (data) { alert(“counter updated!”); }); } </script> </head> <body> <button onclick=”call_counter(‘http://www.google.com’, 12345);”> I update object 12345 </button> <button onclick=”call_counter(‘http://www.yahoo.com’, 999);”> … Read more

Override a form in Django admin

You can override forms for django’s built-in admin by setting form attribute of ModelAdmin to your own form class. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin It’s also possible to override form template – have a look at https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options If you’re looking specifically for autocomplete I can recommend https://github.com/crucialfelix/django-ajax-selects

Django 1.11 TypeError context must be a dict rather than Context

In Django 1.8+, the template’s render method takes a dictionary for the context parameter. Support for passing a Context instance is deprecated, and gives an error in Django 1.10+. In your case, just use a regular dict instead of a Context instance: message = get_template(’email_forms/direct_donation_form_email.html’).render(ctx) You may prefer to use the render_to_string shortcut: from django.template.loader … Read more

How does the order of mixins affect the derived class?

The MRO is basically depth-first, left-to-right. See Method Resolution Order (MRO) in new style Python classes for some more info. You can look at the __mro__ attribute of the class to check, but FooMixin should be first if you want to do “check A” first. class UltimateBase(object): def dispatch(self, *args, **kwargs): print ‘base dispatch’ class … Read more