Skip to main content

Posts

Showing posts with the label view

AJAX form in Django with jQuery.form plugin

Here I will describe an example form using jQuery Form  plugin. It will provide a good frontend, while Django serving a simple backend. I made this AJAX form using unobtrusive javascript. With human language it means the form will function with JavaScript disabled. And so jQuery.form plugin will work as an AJAX speedup addition and not as a major requirement. Features: * Works both with JavaScript  and without. * Uses standard Django ideology/hooks. * Uses the most known jQuery plugin for forms AJAX handling. Theory: Main idea that Django supports both normal and AJAX request in a standard view. And has a handy request.is_ajax() request method. It returns True/False depending on if request search has HTTP_X_REQUESTED_WITH header for the string 'XMLHttpRequest' . jQuery.form plugin sure does have that. Backend Django. We will create an app called 'contact' for our task and place it in the example django project. Urls from the project will redirect to ...

Django: Unittest for HttpResponseRedirect method recipe

I like to invent bicycles in my code. They often come in handy and you basically do not rely on Django version... So upon updating your project's Django version in future times you will not have to refactor half of all the code. So... How do you test http redirects? I usually restrict my time in thinking and write code like if redirect target is hardcoded. And it really is in most of the times. But imagine if you decide to redirect your view response in several places. Or imagine if you're not the only one who owns you'r project's code and there are many collaborators that can change things without warning... And you need to have a test for that view that will test redirects and complex code behavior. I used to check redirects like so: response = self . cliet . get ( '/myview' ) self . assertEqual ( response . status_code , 302 ) # View redirected... All ok... But  if you have some view that can redirect to things that you need to check. How would y...

Users, Groups and their Permissions in Django + Recipes

Django supports security models and methods out of the box. They are Group and Permission objects. Permission is m2m related to internal Django User. I helps you relay on request.user later in your code. You often come to situations where you may need a view to be accessed only by certain group of users.  For example you have the app that has two groups of users. One can search and another one can Index files. Simplest approach is to use Groups here.  In fact you may use permissions in case your app will have several unique users that might do some stuff. In general best approach is to use Group to specify type of users and Permission to specify the role of users in this group. So if you will have Group called 'search' and it will have permission with name, say 'search stuff'. So when you will call: def my_view (request): # ... my view actions ... user_permissions = request.user.user_permissions.all() for p in user...

Django: Rotate Image with PIL usage.

Let's assume we have this simple model: from django.db import models class Image(models.Model): image = models.FileField(upload_to="images/")   Task is to take this simple image, rotate it and save instead of original. Let's discover this way to do so: #views.py from PIL import Image as PilImage from models import Image def rotate(request): #getting instance of the model item=Image.objects.get(pk=1) #opening image for PIL to access im = PilImage.open(image.image) #rotating it by built in PIL command rotated_image = im.rotate(270) #saving rotated image instead of original. Overwriting is on. rotated_image.save(item.image.file.name, overwrite=True) return HttpResponse(str(image.image)) This pattern will work in most cases.  Instead of rotate you can use any other PIL editing method, as for e.g. flip or sharpen the image... Have fun coding and please leave me a comment if you'll find this info useful. ...