There was a task to submit form with ajax, with server side validation of course. Obvious solution is to do validation and return json with erros.
I didn't like idea of writing separate view for validation and then inserting errors in form html on client side. Especially since I already had a generic template for django form with errors display.
User will see process like this:
Server will return piece of html that will replace part of page with article or form (with errors if needed). Actually this sentence is the most important part of this blog post. Implementation is very simple and straightforward.
Well actually the fact that you can return parts of html from server is also not new idea at all. It's just only now I finally understood that using this technique you can create more complicated things than modal window or tabs
def edit_new(request, new_id): new = get_object_or_404() if request.method == 'POST': form = NewsForm(request.POST, instance=new) if form.is_valid(): new = form.save() return render_to_response('include/new.html', dict(new=new)) else: form = NewsForm(instance=new) return render_to_response('include/form.html', dict(form=form, new=new))
Code can be cleaned up of course, I just wanted to show the idea.
I'm translating this article almost five years after it was originally written and of course now it seems very childish :) Now we have angular and ajax forms are implemented a little differently. Still this old solution is not all that bad. I'd still replace one thing here - it's better to return json with html from server instead of just html.
In my current project I had a task to use twitter API. Twitter uses OAuth for authentication, which is pretty dreary. To avoid fiddling with it all the time, I've moved authentication to decorator. If key is available - nothing happens, just view is launched as usual. It's convenient that there's no need for additional twitter settings in user profile. Code is in article.
Lead management is an important part of the marketing strategy of every company of any size. Besides automating various business processes, privately-held organizations should consider implementing an IT solution that would help them manage their leads. So, how should you make a web-based leads management system for a University in order to significantly increase sales?
As you know, model managers can be overriden in Django. It's convenient to add custom filtration method there:Article.objects.published()Article.objects.old()But these custom methods cannot be chained:Article.objects.published().old()In this article I'll explain how this can be solved.