Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Ports and Adapters in python - part one
First part of series about Django application made using Ports and Adapters design pattern. -
Deploying a Django Website on Heroku
Photo by Frances Gunn Once you have a working project, you have to host it somewhere. One of the most popular deployment platforms nowadays is Heroku. Heroku belongs to a Platform as a Service (PaaS) category of cloud computing services. Every Django project you host on Heroku is running inside a smart container in a fully managed runtime environment. Your project can scale horizontally (adding more computing machines) and you pay for what you use starting with a free tier. Moreover, you won't need much of system administrator's skills to do the deployment - once you do the initial setup, the further deployment is as simple as pushing Git repository to a special heroku remote. However, there are some gotchas to know before choosing Heroku for your Django project: One uses PostgreSQL database with your project. MySQL is not an option.You cannot store your static and media files on Heroku. One should use Amazon S3 or some other storage for that.There is no mailing server associated with Heroku. One can use third-party SendGrid plugin with additional costs, GMail SMTP server with sent email amount limitations, or some other SMTP server.The Django project must be version-controlled under Git.Heroku works with Python … -
Ratchets & Levers
There are a couple of metaphors that tend to guide my thinking about the practice of security: ratchets and levers. Ratchets Dr. Schorsch, CC-BY-SA 3.0, via Wikimedia Commons A ratchet is a kind of one-way gear, with angled teeth and a pawl that allows motion in one direction only. In the physical world we use ratchets to help lift or move heavy loads. Using a ratchet, we can overcome the massive inertia of a heavy object by breaking the movement down into small, easy, irreversible steps. -
Conditional Python Dependencies
Since the inception of Python wheels that install without executing arbitrary code, we needed a way to encode conditional dependencies for our packages. Thanks to PEP 426 and PEP 508 we do have a blessed way but sadly the prevalence of old setuptools versions makes them a minefield to use. -
Django Channels for Background Tasks
Django Channels is the most exciting thing to happen to Django since well Django :). This little tutorial is what you need to add a background task processor to Django using channels. Our task for this example will just be outputting "Hello, Channels!", but you could image running a subprocess on some data or sending an email. NOTE: channel works on an at-least-once delivery model, so it is possible a message in a channel could be lost, delivery isn't guaranteed. That also means consumers don't have to worry about duplicates. This example will be stripped down to the basic code without much error checking. There are detailed examples one here and here. We will start with a simple Django 1.9 app without channels # urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.home, name='home'), ] # views.py from django.shortcuts import render def home(request, template="home.html"): print("Hello, Channels!") # long running task of printing. return render( request, template, dict(), ) You will need to define a home.html template where Django can find it, but besides that this simple site should work and synchronously render what is in your home.html and output "Hello, Channels!" on your terminal. Now … -
How to migrate your existing Django project to Heroku
Recently I had some fun with Heroku, the well known PaaS provider. I had a small personal Django project I use for invoicing that I ran locally with ./manage.py runserver when needed. That was a perfect candidate for the Heroku free plan because I need to access the app only occasionally. In this tutorial I assume you have a basic knowledge of what Heroku is, and that you already know how to create and deploy a Python project on Heroku. In case you miss some basic information you can refer to the good Getting started tutorial on Heroku with Python. Here I focus on my use case, which was to migrate an existing Django project on Heroku platform. My existing Django project was structured in accordance to the best practices I read in the wonderful Two Scoops of Django book, so my project structure was similar to this: django/ ├── project │ ├── __init__.py │ ├── settings │ │ ├── __init__.py │ │ ├── base.py │ │ ├── local.py │ │ └── production.py │ ├── urls.py │ ├── wsgi.py ├── app │ ├── __init__.py │ ├── admin.py │ ├── models.py │ ├── tests.py │ ├── urls.py │ ├── views.py └── … -
How to migrate your existing Django project to Heroku
Recently I had some fun with Heroku, the well known PaaS provider. I had a small personal Django project I use for invoicing that I ran locally with ./manage.py runserver when needed. That was a perfect candidate for the Heroku free plan because I need to access the app only occasionally. In this tutorial I assume you have a basic knowledge of what Heroku is, and that you already know how to create and deploy a Python project on Heroku. In case you miss some basic information you can refer to the good Getting started tutorial on Heroku with Python. Table of Contents How to structure your Django project for Heroku How to configure your Django project for Heroku Create an Heroku application for your Django project Migrating data Media files on AWS S3 How to structure your Django project for Heroku Here I focus on my use case, which was to migrate an existing Django project on Heroku platform. My existing Django project was structured in accordance to the best practices I read in the wonderful Two Scoops of Django book, so my project structure was similar to this: django/ ├── project │ ├── __init__.py │ ├── settings │ … -
Dynamically Adding Google Maps with Marker In Django
Google Maps allows you to display maps on your website, we can also customize maps, and the information on maps. The Google Maps API is a JavaScript library. It can be added to a web page with the following script tags: We are creating a div to holds the google map. Here we are also giving an option to search the place on a google map. then add a DOM listener that will execute the getGoogleMap() function on window load (when the page is initially loaded): google.maps.event.addDomListener(window, "load", getGoogleMap) In the above example, we are already loading 3 markers in Bangalore, Chennai, Hyderabad. Then again if a user marks any location, it will display the marker with longitude, latitude of the place(if u want to store) by deleting user previously selecting markers. We can also set the position description dynamically using the info window -
Integration Of GitHub API with python django
Using Github integration by Django, we can get the user verified email id, general information, git hub URL, id, disk usage, public, private repo's, gists and followers, following in a less span of time. These Following steps are needed for Github integration: 1. creating git hub app 2. Authenticating user and getting an access token. 3. Get user information, work history using access token. 1. Creating Github App a. To create an app, click on create an application on top of a page. Here you can give application name then the application will be created. b. Now you can get the client id, secret of an application and you can give redirect urls of your applications. 2. Authenticating user and getting an access token. a. Here We have to create a GET request for asking user permission. POST "https://github.com/login/oauth authorize?client_id=GIT_APP_ID&redirect_uri=REDIRECT_URL&scope=user,user:email&state=dia123456789ramya" GIT_APP_ID: your application client id, SCOPE: List of permissions to request from the person using your app REDIRECT_URI: The url which you want … -
Implement search with Django-haystack and Elasticsearch Part-I
Haystack works as search plugin for django. You can use different back ends Elastic-search, Whose, Sorl, Xapian to search objects. All backends work with same code. In this post i am using elasticsearch as backend. Installation: pip install django-haystack Configuration: add haystack to installed apps INSTALLED_APPS=[ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', #add haystack here 'haystack', 'books' ] Settings: Add back-end settings for haystack. HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', 'URL': 'http://127.0.0.1:9200/', 'INDEX_NAME': 'haystack_books', }, } Above settings for elastic search. Add signal processor for haystack. This signal will update objects in index. HAYSTACK_SIGNAL_PROCESSOR = … -
Setting Up Coveralls for Django Project
Why coveralls? Coveraslls will check the code coverage for your Django project test cases. To use coveralls.io your code must be hosted on GitHub or BitBucket. install coveralls pip install coveralls Using Travis If you are using Travis for you CI. add below script in .travis.yml file in project root folder language: python # python versions python: - "3.4" - "2.7.4" env: -DJANGO=1.8 DB=sqlite3 # install requirements install: - pip install -r requirements.txt - pip install coveralls # To run tests script: - coverage run --source=my_app1, my_app2 manage.py test # send coverage report to coveralls after_success: coveralls Signup with GitHub in https://coveralls.io/ and activate coveralls for you repo. Thats it. Happy Testing... -
Extract text with OCR for all image types in python using pytesseract
What is OCR? Optical Character Recognition(OCR) is the process of electronically extracting text from images or any documents like PDF and reusing it in a variety of ways such as full text searches. In this blog, we will see, how to use 'Python-tesseract', an OCR tool for python. pytesseract: It will recognize and read the text present in images. It can read all image types - png, jpeg, gif, tiff, bmp etc. It’s widely used to process everything from scanned documents. Installation: $ sudo pip install pytesseract Requirements: * Requires python 2.5 or later versions. * And requires Python Imaging Library(PIL). Usage: From the shell: $ ./pytesseract.py test.png Above command prints the recognized text from image 'test.png'. $ ./pytesseract.py -l eng test-english.jpg Above command recognizes english text. In Python Script: import Image from tesseract import image_to_string print image_to_string(Image.open('test.png')) print image_to_string(Image.open('test-english.jpg'), lang='eng') To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code -
How to Create your own e-commerce shop using Django-Oscar.
Oscar is an open-source ecommerce framework for Django. Django Oscar provides a base platform to build an online shop. Oscar is built as a highly customisable and extendable framework. It supports Pluggable tax calculations, Per-customer pricing, Multi-currency etc. 1. Install Oscar $ pip install django-oscar 2. Then, create a Django project $ django-admin.py startproject <project-name> After creating the project, add all the settings(INSTALLED_APPS, MIDDLEWARE_CLASSES, DATABASES) in your settings file And you can find the reference on how to customize the Django Oscar app, urls, models and views here. Customising/Overridding templates: To override Oscar templates, first you need to update the template configuration settings as below in your setting file. import os location = lambda x: os.path.join( os.path.dirname(os.path.realpath(__file__)), x) TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', 'django.template.loaders.eggs.Loader', ) from oscar import OSCAR_MAIN_TEMPLATE_DIR TEMPLATE_DIRS = ( location('templates'), OSCAR_MAIN_TEMPLATE_DIR, ) Note: In the 'TEMPLATE_DIRS' setting, you have to include your project template directory path first and then comes the Oscar's template folder which you can import from oscar. By customising templates, you can just replacing all the content with your own content or you can only change blocks using "extends" Ex: Overriding Home page {% extends 'oscar/promotions/home.html' %} {% block content %} Content goes here … -
Mark Lavin to Give Keynote at Python Nordeste
Mark Lavin will be giving the keynote address at Python Nordeste this year. Python Nordeste is the largest gathering of the Northeast Python community, which takes place annually in cities of northeastern Brazil. This year’s conference will be held in Teresina, the capital of the Brazilian state of Piauí. -
NGINX for static files for dev python server
When you work on the backend part of django or flask project and there are many static files, sometimes the development server becomes slow. In this case it’s possible to use nginx as reverse proxy to serve static. I’m using nginx in docker and the configuration is quite simple. Put in some directory Dockerfile and default.conf.tmpl. Dockerfile 1 2 3 4 5 FROM nginx:1.9 VOLUME /static COPY default.conf.tmpl /etc/nginx/conf.d/default.conf.tmpl EXPOSE 9000 CMD envsubst '$APP_IP $APP_PORT' < /etc/nginx/conf.d/default.conf.tmpl > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;' default.conf.tmpl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 server { listen 9000; charset utf-8; location /site_media { alias /static; } location / { proxy_pass http://${APP_IP}:${APP_PORT}; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } Build image with docker build -t dev-nginx . command. To run it: 1 docker run --rm -it -v `pwd`/static:/static -p 9000:9000 -e APP_IP=<your ip from ifconfig> -e APP_PORT=8000 dev-nginx Then you can access your development server though http://<localhost|docker-machine-ip>:9000. -
Using Django's built in signals and writing custom signals.
Django has a beautiful feature of signals which will record all the actions performed on the particular model. In the current blog post, we’ll learn how to use Django's built-in signals and how to create custom signal Using Django’s built in Signals: Django has a lot of built-in signals like pre_save, post_save, pre_delete and post_delete and etc., For more information about Django's built-in signals visit https://docs.djangoproject.com/en/1.9/ref/signals/. Now we’ll learn how to use Django's pre_delete signal with a simple example. In the way we use pre_delete in the present blog post we can use other signals also in the same way. We have two models called Author and Book their models are defined in models.py as below. # In models.py from django.db import models class Author(models.Model): full_name = models.CharField(max_length=100) short_name = models.CharField(max_length=50) class Book(models.Model): title = models.CharField(max_length=100) slug = models.SlugField(max_length=100) content = model.TextField() status = models.CharField(max_length=10, default=”Drafted”) author_id = model.PositiveIntegerField(null=True) In the above two models we are not having an author as foreignKey to Book model, so by default when the Author gets deleted it won’t delete all the Books written by the author. This is the … -
Pygrunn: Micropython, internet of pythonic things - Lars de Ridder
(One of my summaries of the one-day 2016 PyGrunn conference). micropython is a project that wants to bring python to the world of microprocessors. Micropython is a lean and fast implementation of python 3 for microprocessors. It was funded in 2013 on kickstarter. Originally it only ran on a special "pyboard", but it has now been ported to various other microprocessors. Why use micropython? Easy to learn, with powerful features. Native bitwise operations. Ideal for rapid prototyping. (You cannot use cpython, mainly due to RAM usage.) It is not a full python, of course, they had to strip things out. "functools" and "this" are out, for instance. Extra included are libraries for the specific boards. There are lots of memory optimizations. Nothing fancy, most of the tricks are directly from compiler textbooks, but it is nice to see it all implemented in a real project. Some of the supported boards: Pyboard The "BBC micro:bit" which is supplied to 1 million school children! Wipy. More of a professional-grade board. LoPy. a board which supports LoRa, an open network to connect internet-of-things chips. Development: there is one full time developer (funded by the ESA) and two core contributors. It is stable and … -
Pygrunn: Kliko, compute container specification - Gijs Molenaar
(One of my summaries of the one-day 2016 PyGrunn conference). Gijs Molenaar works on processing big data for large radio telescopes ("Meerkat" in the south of Africa and "Lofar" in the Netherlands). The data volumes coming from such telescopes are huge. 4 terabits per seconds, for example. So they do a log of processing and filtering to get that number down. Gijs works on the "imaging and calibration" part of the process. So: scientific software. Which is hard to install and fragile. Especially for scientists. So they use ubuntu's "lauchpad PPA's" to package it all up as debian packages. The new hit nowadays is docker. Containerization. A self-contained light-weight "virtual machine". Someone called it centralized agony: only one person needs to go through the pain of creating the container and all the rest of the world can use it... :-) His line of work is often centered around pipelines. Data flows from one step to the other and on to the next. This is often done with bash scripts. Docker is nice and you can hook up multiple dockers. But... it is all network-centric: a web container plus a database container plus a redis container. It isn't centered on data … -
Pygrunn: django channels - Bram Noordzij/Bob Voorneveld
(One of my summaries of the one-day 2016 PyGrunn conference). Django channels is a project to make Django to handle more than "only" plain http requests. So: websockets, http2, etc. Regular http is the normal request/response cycle. Websockets is a connection that stays open, for bi-directional communication. Websockets are technically an ordered first-in first-out queue with message expiry and at-most-once delivery to only one listener at the time. "Django channels" is an easy-to-understand extension of the Django view mechanism. Easy to integrate and deploy. Installing django channels is quick. Just add the application to your INSTALLED_APPS list. That's it. The complexity happens when deploying it as it is not a regular WSGI deployment. It uses a new standard called ASGI (a = asynchronous). Currently there's a "worker service" called daphne (build in parallel to django channels) that implements ASGI. You need to configure a "backing service". Simplified: a queue. They showed a demo where everybody in the room could move markers over a map. Worked like a charm. How it works behind the scenes is that you define "channels". Channels can recieve messages and can send messages to other channels. So you can have channel for reading incoming messages, do … -
Pygrunn: Understanding PyPy and using it in production - Peter Odding/Bart Kroon
(One of my summaries of the one-day 2016 PyGrunn conference). pypy is "the faster version of python". There are actually quite a lot of python implementation. cpython is the main one. There are also JIT compilers. Pypy is one of them. It is by far the most mature. PyPy is a python implementation, compliant with 2.7.10 and 3.2.5. And it is fast!. Some advantages of pypy: Speed. There are a lot of automatic optimizations. It didn't use to be fast, but since 5 years it is actually faster than cpython! It has a "tracing JIT compiler". Memory usage is often lower. Multi core programming. Some stackless features. Some experimental work has been started ("software transactional memory") to get rid of the GIL, the infamous Global Interpreter Lock. What does having a "tracing JIT compiler" mean? JIT means "Just In Time". It runs as an interpreter, but it automatically identifies the "hot path" and optimizes that a lot by compiling it on the fly. It is written in RPython, which is a statically typed subset of python which translates to C and is compiled to produce an interpreter. It provides a framework for writing interpreters. "PyPy" really means "Python written in … -
Pygrunn: simple cloud with TripleO quickstart - K Rain Leander
(One of my summaries of the one-day 2016 PyGrunn conference). What is openstack? A "cloud operating system". Openstack is an umbrella with a huge number of actual open source projects under it. The goal is a public and/or private cloud. Just like you use "the internet" without concerning yourself with the actual hardware everything runs on, just in the same way you should be able to use a private/public cloud on any regular hardware. What is RDO? Exactly the same as openstack, but using RPM packages. Really, it is exactly the same. So a way to get openstack running on a Red Hat enterprise basis. There are lots of ways to get started. For RDO there are three oft-used ones: TryStack for trying out a free instance. Not intended for production. PackStack. Install openstack-packstack with "yum". Then you run it on your own hardware. TripleO (https://wiki.openstack.org/wiki/TripleO). It is basically "openstack on openstack". You install an "undercloud" that you use to deploy/update/monitor/manage several "overclouds". An overcloud is then the production openstack cloud. TripleO has a separate user interface that's different from openstack's own one. This is mostly done to prevent confusion. It is kind of heavy, though. The latest openstack release … -
Pygrunn: from code to config and back again - Jasper Spaans
(One of my summaries of the one-day 2016 PyGrunn conference). Jasper works at Fox IT, one of the programs he works on is DetACT, a fraud detection tool for online banking. The technical summary would be something like "spamassassin and wireshark for internet traffic". Wireshark-like: DetACT intercepts online bank traffic and feeds it to a rule engine that ought to detect fraud. The rule engine is the one that needs to be configured. Spamassassin-like: rules with weights. If a transaction gets too many "points", it is marked as suspect. Just like spam detection in emails. In the beginning of the tool, the rules were in the code itself. But as more and more rules and exceptions got added, maintaining it became a lot of work. And deploying takes a while as you need code review, automatic acceptance systems, customer approval, etc. From code to config: they rewrote the rule engine from start to work based on a configuration. (Even though Joel Spolsky says totally rewriting your code is the single worst mistake you can make). They went 2x over budget. That's what you get when rewriting completely.... The initial test with hand-written json config files went OK, so they went … -
Pygrunn keynote: Morepath under the hood - Martijn Faassen
(One of my summaries of the one-day 2016 PyGrunn conference). Martijn Faassen is well-known from lxml, zope, grok. Europython, Zope foundation. And he's written Morepath, a python web framework. Three subjects in this talk: Morepath implementation details. History of concepts in web frameworks Creativity in software development. Morepath implementation details. A framework with super powers ("it was the last to escape from the exploding planet Zope") Traversal. In the 1990's you'd have filesystem traversal. example.com/addresses/faassen would map to a file /webroot/addresses/faassen. In zope2 (1998) you had "traversal through an object tree. So root['addresses']['faassen'] in python. The advantage is that it is all python. The drawback is that every object needs to know how to render itself for the web. It is an example of creativity: how do we map filesystem traversal to objects?. In zope3 (2001) the goal was the zope2 object traversal, but with objects that don't need to know how to handle the web. A way of working called "component architecture" was invented to add traversal-capabilities to existing objects. It works, but as a developer you need to quite some configuration and registration. Creativity: "separation of concerns" and "lookups in a registry" Pyramid sits somewhere in between. And … -
Pygrunn keynote: the future of programming - Steven Pemberton
(One of my summaries of the one-day 2016 PyGrunn conference). Steven Pemberton (https://en.wikipedia.org/wiki/Steven_Pemberton) is one of the developers of ABC, a predecessor of python. He's a researcher at CWI in Amsterdam. It was the first non-military internet site in Europe in 1988 when the whole of Europe was still connected to the USA with a 64kb link. When designing ABC they were considered completely crazy because it was an interpreted language. Computers were slow at that time. But they knew about Moore's law. Computers would become much faster. At that time computers were very, very expensive. Programmers were basically free. Now it is the other way. Computers are basically free and programmers are very expensive. So, at that time, in the 1950s, programming languages were designed around the needs of the computer, not the programmer. Moore's law is still going strong. Despite many articles claiming its imminent demise. He heard the first one in 1977. Steven showed a graph of his own computers. It fits. On modern laptops, the CPU is hardly doing anything most of the time. So why use programming languages optimized for giving the CPU a rest? There's another cost. The more lines a program has, the … -
Caktus CTO Colin Copeland Invited to the White House Open Police Data Initiative
We at Caktus were incredibly proud when the White House Police Data Initiative invited CTO Colin Copeland to celebrate their first year accomplishments. While at the White House, Colin also joined private breakout sessions to share ideas with law enforcement officials, city staff, and other civic technologists from across the country. Colin is the co-founder of Code for Durham and served as lead developer for OpenDataPolicingNC.com. OpenDataPolicingNC.com, a site built for the Southern Coalition for Social Justice, displays North Carolina police stop data.