Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
RevSys Roundup - March 2015
RevSys Roundup - March 2015 -
Twitter way back machine
One of the associates at Techstars created this beautiful demo of Stream’s technology (github repo). If you ever wondered about Thomas Edison’s or Nikola Tesla’s tweets, check it out! :) Share and Enjoy: -
Getting started with Django tastypie
Django tastypie is a library to write RESTful apis in Django. Why use REST You have a database backed web application. This application tracks expenses. The application allows the capability to enter your expenses, view all your expenses, delete an expense etc. Essentially this application provides CRUD functionality. Django application has access to database credentials, but they are never seen by the users of the web application. Django application decides what to show to which user. Django application ensures that a particular user only sees the expenses entered by him and not somebody else's expenses. Now you want to provide a mobile application (Android or iOS) corresponding to this web application. Android application should allow the user to view his expenses, create an expense as well as any other CRUD functionality. But database credentials could not be put in Android code as it is not too hard to decompile an apk and get the db credentials. And we never want a user to get the db credentials else he will be in a position to see everyone's expenses and the entire database. So there has to be another way to allow mobile applications to get things from the database. This … -
Astro Code School Now Accepting Applications - Intermediate Django + Python
I'm really happy to officially announce the first Python and Django Web Engineering class at Astro Code School. I’ll outline some details here and you can also find them on our classes page. This class is twelve weeks long and full time Monday to Friday from 9 AM – 5 PM. It'll be taught here at the Astro Code School at 108 Morris Street, Suite 1b, Durham, NC. We will conduct two Python and Django Web Engineering classes in 2015. The first one in term two starts May 18, 2015 and ends August 10, 2015. The second one in term three starts September 22, 2015 and ends December 15, 2015. Enrollment for both sections opens today March 20. There is space for twelve students in each class. More information about the enrollment process is on our Apply page. Part of that process is an entrance exam that is designed to ensure you're ready to succeed. The price per person for Python and Django Web Engineering is $12,000. The Python and Django Web Engineering class is intended for intermediate level students. Its goal is to help you start your career as a backend web engineer. To start down this … -
Astro Code School Now Accepting Applications - Intermediate Django + Python
I'm really happy to officially announce the first Python and Django Web Engineering class at Astro Code School. I’ll outline some details here and you can also find them on our classes page. This class is twelve weeks long and full time Monday to Friday from 9 AM – 5 PM. It'll be taught here at the Astro Code School at 108 Morris Street, Suite 1b, Durham, NC. We will conduct two Python and Django Web Engineering classes in 2015. The first one in term two starts May 18, 2015 and ends August 10, 2015. The second one in term three starts September 22, 2015 and ends December 15, 2015. Enrollment for both sections opens today March 20. There is space for twelve students in each class. More information about the enrollment process is on our Apply page. Part of that process is an entrance exam that is designed to ensure you're ready to succeed. The price per person for Python and Django Web Engineering is $12,000. The Python and Django Web Engineering class is intended for intermediate level students. Its goal is to help you start your career as a backend web engineer. To start down this … -
Test django view with cookies
To test some view which use cookies: 1 2 3 4 5 6 7 8 9 10 from Cookie import SimpleCookie from django import test class SomeTest(test.TestCase): def test_some_view(self): self.client.cookies = SimpleCookie({'test_cookie': 'test_value'}) response = self.client.get('/some-url/') self.assertEqual(response.client.cookies['test_cookie'].value, 'test_value') -
Django Gotchas #1 - Dynamic Initial Values In Forms!
Django form fields accept initial argument. So You can set a default value for a field.Sometimes it is required to override __init__ method in forms and set field initial arguments.Now let's pass some initial data to form and see what happens.If You look at the value of input field, it's is NOT the overrided. It still has form initial value! If You look into source code of django forms to find what is happening, You will find this.So form's initial value has precedence over fields initial values.So You have to override form's initial value instead of fields's initial value to make it work as expected.Read official docs about django forms.Read more articles about Python! -
Django Tips & Tricks #3 - Dynamic Initial Values In Forms
Django form fields accept initial argument. So You can set a default value for a field. In [1]: from django import forms In [2]: class SampleForm(forms.Form): ...: name = forms.CharField(max_length=10, initial='avil page') ...: In [3]: f = SampleForm() In [4]: f.as_p() Out[4]: u'<p>Name: <input maxlength="10" name="name" type="text" value="avil page" /></p>' Sometimes it is required to override init method in forms and set field initial arguments. In [11]: from django import forms In [12]: class AdvancedForm(forms.Form): ....: ....: def __init__(self, *args, **kwargs): ....: super().__init__(*args, **kwargs) ....: self.fields['name'].initial = 'override' ....: ....: name=forms.CharField(max_length=10) ....: In [13]: f2 = AdvancedForm() In [14]: f2.as_p() Out[14]: '<p>Name: <input maxlength="10" name="name" type="text" value="override" /></p>' Now let's pass some initial data to form and see what happens. In [11]: from django import forms In [12]: class AdvancedForm(forms.Form): ....: ....: def __init__(self, *args, **kwargs): ....: super().__init__(*args, **kwargs) ....: self.fields['name'].initial = 'override' # don't try this at home ....: ....: name=forms.CharField(max_length=10) ....: In [19]: f3 = AdvancedForm(initial={'name': 'precedence'}) In [20]: f3.as_p() Out[20]: '<p>Name: <input maxlength="10" name="name" type="text" value="precedence" /></p>' If You look at the value of input field, it's is NOT the overrided. It still has form initial value! If You look into source code of django forms to … -
Why RapidSMS for SMS Application Development
Caktus has been involved in quite a few projects (Libyan voter registration, UNICEF Project Mwana, and several others) that include text messaging (a.k.a. Short Message Service, or SMS), and we always use RapidSMS as one of our tools. We've also invested our own resources in supporting and extending RapidSMS. There are other options; why do we consistently choose RapidSMS? What is RapidSMS First, what is RapidSMS? It's an open source package of useful tools that extend the Django web development framework to support processing text messages. It includes: A framework for writing code to be invoked when a text message is received and respond to it A set of backends - pluggable code modules that can interface to various ways of connecting your Django program to the phone network to pass text messages back and forth Sample applications Documentation The backends are required because unlike email, there's no universal standard for sending and receiving text messages over the Internet. Often we get access to the messages via a third party vendor, like Twilio or Tropo, that provides a proprietary interface. RapidSMS isolates us from the differences among vendors. RapidSMS is open source, under the BSD license, with UNICEF acting as … -
Why RapidSMS for SMS Application Development
Caktus has been involved in quite a few projects (Libyan voter registration, UNICEF Project Mwana, and several others) that include text messaging (a.k.a. Short Message Service, or SMS), and we always use RapidSMS as one of our tools. We've also invested our own resources in supporting and extending RapidSMS. There are other options; why do we consistently choose RapidSMS? What is RapidSMS First, what is RapidSMS? It's an open source package of useful tools that extend the Django web development framework to support processing text messages. It includes: A framework for writing code to be invoked when a text message is received and respond to it A set of backends - pluggable code modules that can interface to various ways of connecting your Django program to the phone network to pass text messages back and forth Sample applications Documentation The backends are required because unlike email, there's no universal standard for sending and receiving text messages over the Internet. Often we get access to the messages via a third party vendor, like Twilio or Tropo, that provides a proprietary interface. RapidSMS isolates us from the differences among vendors. RapidSMS is open source, under the BSD license, with UNICEF acting as … -
My approach to Class Based Views
I've written in the past about my dislike for Django's Class Based Views. Django's CBVs add a lot of complexity and verbosity, and simply get in the way of some moderately common patterns (e.g. when you have two forms in a single view). It seems I'm not alone as a Django core dev who thinks that way. In this post, however, I'll write about a different approach that I took in one project, which can be summed up like this: Write your own base class. For really simple model views, Django's own CBVs can be a time saver. For anything more complex, you will run into difficulties, and will need some heavy documentation at the very least. One solution is to use a simplified re-implementation of Class Based Views. My own approach is to go even further and start from nothing, writing your own base class, while borrowing the best ideas and incorporating only what you need. Steal the good ideas The as_view method provided by the Django's View class is a great idea — while it may not be obvious, it was hammered out after a lot of discussion as a way to help promote request isolation by creating … -
Saving processes and threads in a WSGI server with Moya
I have a webserver with 3 WSGI applications running on different domains (1, 2, 3). All deployed with a combination of Gunicorn and NGINX. A combination that works really well, but there are two annoyances that are only going to get worse the more sites I deploy: A) The configuration for each server resides in a different location on the filesystem, so I have to recall & type a long path to edit settings. B) More significantly, each server adds extra resource requirements. I follow the advice of running each WSGI application with (2 * number_of_cores + 1) processes, each with 8 threads. The threads may be overkill, but that ensures that the server can use all available capacity to handle dynamic requests. On my 4 core server, that's 9 processes, 72 threads per site. Or 27 processes, and 216 threads for the 3 sites. I suspect that is too many resources. A new feature recently added to Moya fixes both those problems. Rather than deploy a WSGI application for each site, Moya can now optionally create a single WSGI application that serves many sites. With this new system, configuration is read from /etc/moya/, which contains a directory structure like … -
Saving processes and threads in a WSGI server with Moya
I have a webserver with 3 WSGI applications running on different domains (1, 2, 3). All deployed with a combination of Gunicorn and NGINX. A combination that works really well, but there are two annoyances that are only going to get worse the more sites I deploy: A) The configuration for each server resides in a different location on the filesystem, so I have to recall & type a long path to edit settings. B) More significantly, each server adds extra resource requirements. I follow the advice of running each WSGI application with (2 * number_of_cores + 1) processes, each with 8 threads. The threads may be overkill, but that ensures that the server can use all available capacity to handle dynamic requests. On my 4 core server, that's 9 processes, 72 threads per site. Or 27 processes, and 216 threads for the 3 sites. Clearly that's not scalable if I want to host more web applications on one server. A new feature recently added to Moya fixes both those problems. Rather than deploy a WSGI application for each site, Moya can now optionally create a single WSGI application that serves many sites. With this new system, configuration is read … -
Saving processes and threads in a WSGI server with Moya
I have a webserver with 3 WSGI applications running on different domains (1, 2, 3). All deployed with a combination of Gunicorn and NGINX. A combination that works really well, but there are two annoyances that are only going to get worse the more sites I deploy: A) The configuration for each server resides in a different location on the filesystem, so I have to recall & type a long path to edit settings. B) More significantly, each server adds extra resource requirements. I follow the advice of running each WSGI application with (2 * number_of_cores + 1) processes, each with 8 threads. The threads may be overkill, but that ensures that the server can use all available capacity to handle dynamic requests. On my 4 core server, that's 9 processes, 72 threads per site. Or 27 processes, and 216 threads for the 3 sites. Clearly that's not scalable if I want to host more web applications on one server. A new feature recently added to Moya fixes both those problems. Rather than deploy a WSGI application for each site, Moya can now optionally create a single WSGI application that serves many sites. With this new system, configuration is read … -
Documenting Python without Sphinx
Last week Evennia merged its development branch with all the features mentioned in the last post. Post-merger we have since gone through and fixed remaining bugs and shortened the list at a good clip. One thing I have been considering is how to make Evennia's API auto-documenting - we are after all a MUD creation library and whereas our code has always been well-documented the docs were always only accessible from the source files themselves.Now, when you hear "Python" and "documentation" in the same sentence, the first thought usually involves Sphinx or Sphinx autodoc in some form. Sphinx produces very nice looking documentation indeed. My problem is however as follows:I don't want our API documentation to be written in a different format from the rest of our documentation, which is in Github's wiki using Markdown. Our users should be able to help document Evennia without remembering which formatting language is to be used.I don't like reStructuredText syntax. This is a personal thing. I get that it is powerful but it is also really, really ugly to read in its raw form in the source code. I feel the sources must be easy to read on their own. Sphinx plugins like … -
How to build a notification feed using Stream
Tommaso wrote a quick tutorial on how to build a notification feed using Django and Stream. How to build a notification feed using Stream. Share and Enjoy: -
Connecting Django Models with outer applications
Preface: Sometimes, parts of the data that you have to display in your application reside out of the Django models. Simple example for this is the following case - the client requires that you build them a webshop but they already have CRM solution that holds their products info. Of course they provide you with a mechanism to read this data from their CRM. Specialty: The problem is that the data in their CRM does not hold some of the product information that you need. For instance it misses SEO-friendly description and product image. So you will have to set up a model at your side and store these images there. It is easy to join them, the only thing that you will need is a simple unique key for every product. Solution: Here we use the product_id field to make the connection between the CRM data and the Django model. # in models.py class Product(models.Model): product_id = models.IntegerField(_('Original Product'), unique=True) description = models.TextField(_('SEO-friendly Description'), blank=True) pod_image = FilerImageField(verbose_name=_('Product Image'), blank=True, null=True) @property def name(self): return crm_api.get_product_name(self.product_id) # in forms.py class ProductForm(forms.ModelForm): name = forms.CharField(required=False, widget=forms.TextInput(attrs={ 'readonly': True, 'style': 'border: none'})) class Meta: model = Product widgets = { 'product_id': … -
Software for business
I am starting a new blog. The reason is that I want to keep this one more technically oriented while the other will be more business and customers oriented. Its name is Software for business and the idea is to show to the business in less technical details how the modern IT technologies like CRM & ERP systems, web sites, e-commerce solutions, online marketing and so on can help their business. If you find the topic interesting feel free to join. -
Faking attributes in Python classes...
... or how to imitate dynamic properties in a class object Preface: When you have connections between your application and other systems frequently the data is not in the most useful form for your needs. If you have an API it is awesome but sometimes it just does not act the way you want and your code quickly becomes a series of repeating API calls like api.get_product_property(product_id, property). Of course it will be easier if you can use objects to represent the data in you code so you can create something like a proxy class to this API: class Product(object): def __init__(self, product_id): self.id = product_id @property def name(self): return api_obj.get_product_property(self.id, 'name') @property def price(self): return api_obj.get_product_property(self.id, 'price') #usage product = Product(product_id) print product.name In my opinion it is cleaner, more pretty and more useful than the direct API calls. But still there is something not quite right. Problem: Your model have not two but twenty properties. Defining 20 method makes the code look not that good. Not to mention that amending the code every time when you need a new property is quite boring. So is there a better way? As I mention at the end of Connecting Django … -
Simple Site Checker
... a command line tool to monitor your sitemap links I was thinking to make such tool for a while and fortunately I found some time so here it is. Simple Site Checker is a command line tool that allows you to run a check over the links in you XML sitemap. How it works: The script requires a single attribute - a URL or relative/absolute path to xml-sitemap. It loads the XML, reads all loc-tags in it and start checking the links in them one by one. By default you will see no output unless there is an error - the script is unable to load the sitemap or any link check fails. Using the verbosity argument you can control the output, if you need more detailed information like elapsed time, checked links etc. You can run this script through a cron-like tool and get an e-mail in case of error. I will appreciate any user input and ideas so feel free to comment. -
HTTP Status Codes Site
During the development of Simple Site Checker I realised that it would be useful for test purposes if there is a website returning all possible HTTP status codes. Thanks to Google App Engine and webapp2 framework building such website was a piece of cake. The site can be found at http://httpstatuscodes.appspot.com. The home page provides a list of all HTTP status codes and their names and if you want to get an HTTP response with a specific status code just add the code after the slash, example: http://httpstatuscodes.appspot.com/200 - returns 200 OK http://httpstatuscodes.appspot.com/500 - returns 500 Internal Server Error Also at the end of each page is located the URL of the HTTP protocol Status Codes Definitions with detailed explanation for each one of them. The website code is publicly available in github at HTTP Status Codes Site. If you find it useful feel free to comment and/or share it. -
Fabric & Django
Or how automate the creation of new projects with simple script Preface: Do you remember all this tiny little steps that you have to perform every time when you start new project - create virtual environment, install packages, start and setup Django project? Kind of annoying repetition, isn't it? How about to automate it a bit. Solution: Recently I started learning Fabric and thought "What better way to test it in practice than automating a simple, repetitive task?". So, lets mark the tasks that I want the script to perform: Create virtual environment with the project name Activate the virtual environment Download list of packages and install them Make 'src' directory where the project source will reside Create new Django project in source directory Update the settings Thanks to the local command the first one was easy. The problem was with the second one. Obviously each local command is run autonomously so I had to find some way have activated virtual environment for each task after this. Fortunately the prefix context manager works like a charm. I had some issues making it read and write in the paths I wants and voilà it was working exactly as I want. The … -
Python is not a Panacea ...
... neither is any other language or framework This post was inspired by the serial discussion on the topic "Python vs other language"(in the specific case the other one was PHP, and the question was asked in a Python group so you may guess whether there are any answers in favor of PHP). It is very simple, I believe that every Python developer will tell you that Python is the greatest language ever build, how easy is to learn it, how readable and flexible it is, how much fun it is to work with it and so on. They will tell you that you can do everything with it: web and desktop development, testing, automation, scientific simulations etc. But what most of them will forgot to tell you is that it is not a Panacea. In the matter of fact you can also build "ugly" and unstable applications in Python too. Most problems come not from the language or framework used, but from bad coding practices and bad understanding of the environment. Python will force you to write readable code but it wont solve all your problems. It is hard to make a complete list of what exactly you must … -
Django compressor and image preloading
Preface: Have you noticed how on some websites when you click on a link that opens a lightbox or any overlay for first time it takes some time to display the border/background/button images. Not quite fancy, right? This is because the load of this images starts at the moment the overlay is rendered on the screen. If this is your first load and these images are not in your browser cache it will take some time for the browser to retrieve them from the server. Solution: The solution for this is to preload the images i.e. to force the browser to request them from the server before they are actually used. With a simple javascript function and a list of the images URLs this is a piece of cake: $.preLoadImages = function() { var args_len = arguments.length; for (var i=0; i < args_len; i++) { var cacheImage = document.createElement('img'); cacheImage.src = arguments[i]; } } $.preLoadImages('/img/img1.png', '/img/img2.png') Please have in mind that the code above uses the jQuery library. Specialty: Pretty easy, but you have to hardcode the URLs of all images. Also if you are using Django compressor then probably you are aware that it adds extra hash to the … -
The road to hell is paved with regular expressions ...
... or what is the cost of using regular expressions for simple tasks Regular expressions are one of the most powerful tools in computing I have ever seen. My previous post about Django compressor and image preloading is a good example how useful they might be. The only limit of their use is your imagination. But "with great power, comes great responsibility" or in this case a great cost. Even the simplest expressions can be quite heavy compared with other methods. The reason to write about this is a question recently asked in a python group. It was about how to get the elements of a list that match specific string. My proposal was to use comprehension list and simple string comparison while other member proposed using a regular expression. I was pretty sure that the regular expression is slower but not sure exactly how much slower so I made a simple test to find out. import re import timeit my_list = ['abc-123', 'def-456', 'ghi-789', 'abc456', 'abc', 'abd'] def re_check(): return [i for i in my_list if re.match('^abc$', i)] t = timeit.Timer(re_check) print 're_check result >>', re_check() print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) def simple_check(): return [i for i …