Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
How to Crop Images in a Django Application
Cropping images is a fairly common use case in a Web application. For example, some applications let you upload a profile picture. In addition, it usually also let you crop and resize the image for a better result. Unfortunately, when dealing with image processing we need to install a few dependencies, both in the front-end and in the back-end. In this tutorial I will demonstrate how to upload an image, display a preview in a modal, crop the image and finally upload it and save in the server. If you are not familiar with file upload in Django, check this post for a quick reference: How to Upload Files With Django. Installing Dependencies In the following example I will be using: Python 3.6.0 Django 1.10.5 Pillow 4.0.0 jQuery 3.1.1 Cropper v3.0.0-beta Within your project directory or virtualenv, install Pillow: pip install Pillow I know, I know… Sometimes installing Pillow is a nightmare. If you face any problem installing it locally, please refer to the Pillow’s Installation Guide. The jQuery utility to crop images in the Web browser can be downloaded here. Or if you prefer to use it’s pure JavaScript version, download Cropper.js here. But please note that in the … -
Tracking the Results of Cron Jobs
Every Django website needs some automatic background tasks to execute regularly. The outdated sessions need to be cleaned up, search index needs to be updated, some data needs to be imported from RSS feeds or APIs, backups need to be created, you name it. Usually, if not all the time, those regular tasks are being set as cron jobs. However, when some task is run in the background, by default, you don't get any feedback whether it was successfully completed, or whether it crashed on the way. In this post I will show you how I handle the results of cron jobs. In a Django project, all those tasks are usually implemented as management commands. For each such command I write a short bash script, that will call the management command with specific parameters and will print the verbose output to a log file. Let's say my project structure is like this on a remote server: /home/myproject├── bin├── include├── lib├── public_html├── backups├── project│ └── myproject├── scripts└── logs A virtual environment is created in the home directory of myproject linux user. The Django project itself is kept under project directory. The scripts directory is for my bash scripts. And the logs … -
Build Facebook messenger bots with Python and Django
Throughout this tutorial ,we are going to see how to build a Facebook messenger bot using Python and Django framework . The Facebook messenger platform is one of the biggest platforms for messaging on the Internet in these days . If you are a business and your have a Facebook page that it might be a good idea to provide your users with an automatic method to get some answers such as the frequently asked questions that get asked all the time by customers but you can also build more advanced bots that use artificial intelligence (AI) . Here is a list of the best messenger bots Also watch this video from CNNMoney which talks about how Messenger Bots can replace customer service for brands You'll learn how to build a simple Facebook bot step by step .The bot will not use any advanced AI algorithms but only a simple IF based statements so you need to have some knowledge about Python language and Django framework . Creating a Facebook app A Facebook bot is just another type of Facebook apps so we need to create a Facebook app for our bot . Simply go to Facebook developers website then … -
Django deployment webinar starting soon
Today on 16:00 UTC I’m hosting a webinar. Deploying Django: From zero to production in 20 minutes. I’m sorry about the late notice. I’ve announced it on many places and it has generated quite some interest, but it occured to me that some people who are monitoring this blog might not see it elsewhere. See you on the webinar. The post Django deployment webinar starting soon appeared first on Django deployment. -
Django Admin and Celery
A weird race condition Some background: We have a model that is edited only via the Django admin. The save method of the model fires a celery task to update several other records. The reason for using celery here is that the amount of related objects can be pretty big and we decided that it is best to spawn the update as a background job. We have unit and integration tests, the code was also manually tested, everything looked nice and we deployed it. On the next day we found out that the code is acting weird. No errors, everything looked like it has worked but the updated records actually contained the old data before the change. The celery task accepts the object ID as an argument and the object is fetched from the database before doing anything else so the problem was not that we were passing some old state of the object. Then what was going on? Trying to reproduce it: Things are getting even weirder. The issues is happening every now and then. Hm...? Race condition?! Let's take a look at the code: class MyModel(models.Model): def save(self, **kwargs): is_existing_object = False if self.pk else True super(MyModel, self).save(**kwargs) … -
You Sould do Weird Stuff in Django
Out of the ordinary things are the times that we grow the most as developers. However, a lot of times we don't do enough weird things when we write our software. I would encourage you to try that odd idea that you have been messing with in your head that you aren't sure you can do. Ironically you will grow more as a developer from that than your day to day getting stuff done development I am not sure why I ever really thought about this, but give a watch to what keyed me in on this thought. You Should Do Weird Stuff in Django -
Install Django on Mac OS or Linux
# Install Django & Virtualenv ... -
How to build a classified web app with Django
Throughout this tutorial series we are going to build a classified website with Django framework starting from the very first step which is the setup and configuration of development environment to the final step . The resulting project will be hosted on GitHub with an MIT license so feel free to fork or clone it if you need a ready project for tweaking but you’ll need to apply your own CSS styles because it only has minimal styling with Bootstrap framework . If you want to learn how to build your own classified web application from scratch using Python and Django just make sure you follow these series from the start till the end . Getting started with a Django project This tutorial assumes you have already Python and Django installed on your machine . Also i’m developing under a Ubuntu system so the commands and instructions in this tutorial are designed for Ubuntu but you should be able to follow the same steps on MAC or using the command prompt on Windows . So go ahead and create a new virtual environment for our project . Open up the terminal and navigate to your desired location then type virtualenv … -
Make Your Developer's Life Easier By Reducing Number Of Opened Tabs in Pycharm
When coding, I often find myself overwhelmed by a number of opened tabs in my editor. I just seem to have opened every single .py file of a Django project. And it's kinda hard to find a way in this evergrowing of tabs. Given that, I was really happy to ... Read now -
Python type annotations
When it comes to programming, I have a belt and suspenders philosophy. Anything that can help me avoid errors early is worth looking into. -
New in Django 1.11: Template-based widget rendering
There's a very interesting change coming with Django 1.11 - To make customizing widgets easier, form widget rendering is now done using the template system. In older versions, widgets were rendered using Python. You can get more details about it from The form rendering API docs page: https://docs.djangoproject ... Read now -
Mercurial Mirror For Django 1.11 Branch
Here is our usual “production” mirror, aimed at: being a lightweight read-only repository to clone from for production servers hide the ugly git stuff behind a great mercurial interface The clone is at the usual location at bitbucket, from which you can browse, clone, update, … https://bitbucket.org/orzel/django-1.11-production/ -
How to Add reCAPTCHA to a Django Site
Google’s reCAPTCHA is a very popular solution to protect your application or website against bots and spam. It is fairly simple to implement. In this tutorial you will find a working example using only built-in libraries, an alternative using requests and also an implementation using decorators, to reuse the reCAPTCHA verification across your application. Setup First thing, register your application in the reCAPTCHA admin. I added the 127.0.0.1 IP address as my domain for testing purpose. Here you are supposed to add your website domain. After registering your website, you will be handed a Site key and a Secret key. The Site key will be used in the reCAPTCHA widget which is rendered within the page where you want to place it. The Secret key will be stored safely in the server, made available through the settings.py module. settings.py GOOGLE_RECAPTCHA_SECRET_KEY = '6LdRSRYUAAAAAOnk5yomm1dI9BmQkJWTg_wIlMJ_' PS: It is not a good idea to keep this kind of information directly in the settings.py. I’m adding it here so the example is more explicit. Please refer to this article Package of the Week: Python Decouple to learn how to separate configuration from settings, and keep sensitive information in a safe place. Implementing the reCAPTCHA Let’s … -
Why You Should Pin Your Dependencies by My Mistakes
Have you ever been bitten by not pinning your dependencies in your django project? If not be glad, and come learn from my problems. Pinning your dependencies is important to solve future unknown issues, better the devil you know and all that. In this weeks video I talk about 3 times I had issues. They are either not pinning my dependencies, a weird edge case with pinning and python, and not really understanding what I was doing with pinned dependencies. Why You Should Pin Your Dependencies -
How to Create User Sign Up View
In this tutorial I will cover a few strategies to create Django user sign up/registration. Usually I implement it from scratch. You will see it’s very straightforward. For the examples I will use an empty Django project named mysite. Inside the mysite folder I created an app named core. So every time you see mysite and/or core, change to the suitable project name and app name. A brief summary of what you are going to find here: Basic Sign Up Sign Up With Extra Fields Sign Up With Profile Model Sign Up With Confirmation Mail Basic Sign Up The most simple way to implement a user sign up is by using the UserCreationForm as it is. This strategy is suitable in case you are using the default Django user, using username to authenticate and is interested only in setting the username and password upon sign up. urls.py from django.conf.urls import url from mysite.core import views as core_views urlpatterns = [ ... url(r'^signup/$', core_views.signup, name='signup'), ] views.py from django.contrib.auth import login, authenticate from django.contrib.auth.forms import UserCreationForm from django.shortcuts import render, redirect def signup(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') user = … -
Caktus Attends Wagtail CMS Sprint in Reykjavik
Caktus CEO Tobias McNulty and Sales Engineer David Ray recently had the opportunity to attend a development sprint for the Wagtail Content Management System (CMS) in Reykjavik, Iceland. The two-day software development sprint attracted 15 attendees hailing from a total of 5 countries across North America and Europe. -
HTML Template to PDF in Django
** Still in Development ** ... -
Create a Blank Django Project
A easy to use guide for creati... -
History of the URL, or why you are confused by serving many domains with a single server
I’ve noticed that many people attempting to deploy Django have trouble understanding how a single web server installation can serve many domains. One reason is that words matter. What Apache calls a “virtual host” is exactly what nginx calls a “server” and what HTTP calls a “host”. It is neither a host (let alone a virtual one) nor a server; it’s a domain. Let’s clear this up. The first version of the HyperText Transfer Protocol was very simple. If your browser wanted to visit page http://djangodeployment.com/apage, it would connect to the server djangodeployment.com, port 80, and after the TCP connection was established it would send this line to the server, terminated with a newline: GET /apage The server would then respond with the content of that page and close the connection. The content of the page was usually something like this: <html> <head> <title>Hello</title> </head> <body> <p>hello, world</p> </body> </html> In that era, a single computer could not distinguish different domains in the request. Whether you visited http://djangodeployment.com/apage or http://71.19.145.109/apage you’d get the same response. This is because the whole syntax of the URL, protocol://host[:port]/path, assumed you would get a path from a server. In that context, “host” is synonymous … -
Different types of testing in Django
Testing is always one of those topics that can be interesting to talk about. There are a lot of different opinions on testing so it can be fun. Django comes with some great tools for testing, so this week we will talk a little bit about the different types of tests. Then expand on that with how that relates to Django. I also present to you a new type of test at the end of the video that I have been using, special thanks to a co-worker for coming up with the idea. It is really specific to django, and I haven't heard of others doing it. Different Types of Tests with Django -
Podcasttime.io - How Much Time Do Your Podcasts Take To Listen To?
It's a web app where you search and find the podcasts *you* listen to. It then gives you a break down how much time that requires to keep up per day, per week and per month. -
Django - migrating from function based views to class based views
The single most significant advantage in Django class-based views is inheritance. On a large project, it's likely that we will have lots of similar views. Instead of writing the repeated code we can simply have our views inherit from a base view. Also, Django ships with a collection of generic view classes that can be used to do some of the most common tasks. 1. Template View Function based view urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^about-us/$', views.about_us, name="about_us"), ] views.py from django.shortcuts import render def about_us(request): return render(request, 'templates/contact.html') Class based view urls.py from django.conf.urls import url from .views import AboutUs urlpatterns = [ url(r'^about-us/$', AboutUs.as_view(), name="about_us"), ] views.py from django.views.generic import TemplateView class AboutUs(TemplateView): template_name = "templates/about.html" or we can directly write it in "urls.py" urls.py from django.conf.urls import url from django.views.generic import TemplateView urlpatterns = [ url(r'^about-us/$', TemplateView.as_view(template_name= "templates/about.html"), name="about_us"), ] 2. Detail View Function based view urls.py from django.conf.urls import url from . … -
Set Up Travis CI For Django project
Travis CI is a continuous integration service used to build and test applications hosted on GitHub. Travis CI supports integration with other tools such as coverage analyzers. Why use Travis? Travis CI runs your program's tests every time you commit to GitHub. you can easily discover your code breaks. setting up Travis-ci is very easy. To run tests in Travis you have to create ".travis.yml" file in root directory. .travis.yml file for Django application. language: python # => 1 python: # => 2 - "2.6" - "2.7" services: # => 3 - mysql env: # => 4 -DJANGO=1.8 DB=mysql install: # => 5 - pip install -r requirements.txt before_script: # => 6 - mysql -e 'create database test;' -u root script: # => 7 - python manage.py test Explanation for above comments: 1. By defining "language: python" application is developed in python language. 2. Test your application in multiple versions of python by defining versions in python hook settings 3. Define services required for your application ex: elastic … -
Deploying Your Django app on Heroku
Heroku is a cloud application platform it's a new way of building and deploying web apps, Which makes easy to host your application in the cloud with a simple git push command. Heroku supports several programming languages like(Python, Java, PHP) Install the Heroku Toolbelt: The first thing you need to do is to install the Heroku toolbelt. In order to interact with heroku service, the toolbelt is best command line software. Here you can find out the Heroku toolbelt installation for Debian/Ubuntu, Run this from your terminal: wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh In the below link you need to select your required operating system(Download Heroku Toolbelt for..) for installing heroku toolbelt in your system. And then you can proceed to install. Please click here to install Heroku ToolBelt After successfully installation of toolbelt, you can use the heroku command from your terminal. heroku login You will prompt to provide heroku credentials(Email and password), once you have authenticated you can access both heroku and git commands. Create your heroku app: The following command is used to create an app heroku create your-app-name Here 'your-app-name' should be unique, heroku will generate default app-name if you won't specify any app-name. For creating remote … -
Django Unit Test cases with Forms and Views
Test Cases For Forms, Views In this post, we’ll see how to write unit test cases for any project. Having tests for any project will helps you to find bugs. If any of the function breaks, you will know about it. Its easier to debug code line by line. Unit Tests: Unit Tests are isolated tests that test one specific function. Test Case: A test case is executing set of features for your Application. Proper development of test cases finds problems in your functionality of an Application. Test suite: A test suite is a collection of test cases. It is used to aggregate tests that should be executed together. In general, tests result in either a Success (expected results), Failure (unexpected results), or an error. While writting test cases, not only testing for the expected results but also need to test how good your code handles for unexpected results. Testing the Forms: Consider a Form: from django import forms from .models import * class UserForm(forms.ModelForm): class Meta: model = User fields = ('email', 'password', 'first_name', 'phone') setUp(): The setUp() methods allows to define instructions which will be executed before and after …