Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django NGINX: deploy your Django project on a production server
Django NGINX is a popular and well tested combination used to deploy web applications in production. In this post I will explain the steps needed to have your Django project deployed on a production server using Ubuntu 18.04. To have Django NGINX deployed on your production server follow these simple steps. Table of Contents Django Nginx: it's better together! Install required packages using apt Create directories for your static and media files Setup your Django project and install requirements Collect static files Configure uWSGI to host your Django project Configure NGINX to serve your application Enjoy your Django NGINX application Extra step: automate all these steps with Ansible! Django Nginx: it's better together! Django is a web framework written in Python. It lets you develop and prototype web applications with little effort. It comes with many built-in features that are often useful in a web application, such as: session management, user authentication, an object relational mapper, a template language. NGINX is a very fast and configurable web server that is very well suited to serve static files and act as a reverse proxy for other applications. In this tutorial I'll explain how to configure NGINX to serve static files (images, … -
Django DetailView - podstawowy widok generyczny
DetailView w Django to podstawowy widok generyczny. Poznaj go koniecznie! -
My Python Development Environment, 2020 Edition
For years I’ve noodled around with various setups for a Python development environment. A couple of years ago I wrote about a setup I finally liked; this is an update to that post. Bad news: this stuff still isn’t stable, and I’ve had to make some changes. Good news: the general concepts still hold, and the new tools a generally a bit better. If you’re curious about the changes and why I made them, there’s a section at the very end about that. -
Full Text Search in Django
A detailed post describing how to implement search in Django. -
Full Text Search in Django
A detailed post describing how to implement search in Django. -
Full Text Search Django
A detailed post describing how to implement search in Django. Today we are going to be talking about Full Text Search in Django. Now even though Django is a batteries included web framework, it does not come with an inbuilt search functionality, there isn’t one perfect way to implement search in your web application provided by it, there are many different ways in which it can be done, today we are gonna be talking about a few of those methods, let’s start by first listing out the methods which we can utilise, I will be giving you a brief overview of each method as well: Basic : When you have a small scale app and you just wanna implement a search view that can be used to filter out the items based on user’s query without any added intelligence, you can utilise the filter function provided within the Django ORM itself, or there’s another thing called Q object present in it, which you can use for that purpose as well. We’ll discuss the two in the subsequent sections. Full Text Search : Django provides Full Text Search ability included in the django.contrib.postgres module, now going by its name you must … -
Random, Fixed Ordering and Pagination
Consider the following situation: > We have a set of items that we want to show in a random order. > The order, however, should remain fixed for a period. > The display of items will need to be paginated, and we have over 200,000 items. If the ordering is not truly random, then we could have an expression index, and allow ordering based on that. However, that doesn't really help out with the pagination, and issues around LIMIT/OFFSET ordering of very large sets. Instead, I came up with a solution this afternoon that uses Postgres Materialised Views. Let's start with a Django model: {% highlight python %} class Post(models.Model): title = models.TextField() content = models.TextField() posted_at = models.DateTimeField() {% endhighlight %} We can build up a materialised view that associates each post with a position: {% highlight postgresql %} CREATE MATERIALISED VIEW post_ordering AS SELECT post.id AS post_id, row_number() OVER () AS position FROM ( SELECT id FROM blog_post ORDER BY random() ) post; CREATE INDEX post_ordering_id ON post_ordering(post_id); CREATE INDEX post_ordering_position ON post_ordering(position); {% endhighlight %} Because a materialised view stores a copy of the data, we need to index it if we want to get performance benefits. … -
Channels
Weekly Django Chat newsletterdjango-channelsDjangoCon 2019 - Just Add Await: Retrofitting Async Into Django by Andrew GodwinDeveloping a Real-Time Taxi App with Django Channels and Angularhttpxdeps: Django Enhancement ProposalsDjango Developers Google GroupDjango Forum -
Start Learning Web Development
There is a LOT of good information out there. Tutorials, videos, paid courses, free courses… so much good stuff. When I decided I wanted to learn to code,… -
Start Learning Web Development
Skip to tl;dr There is a LOT of good information out there. Tutorials, videos, paid courses, free courses… so much good stuff. When I decided I wanted to learn to code, I just started doing whatever looked interesting to me. It’s easy to fall into the trap of trying to pick the “optimal” syllabus for your personal learning journey. I wanted to start coding now. I actually started years ago – overnight in an airport in Bergen, Norway – doing the Django polls tutorial. This is a good one, but my goodness it was a hard time. Yes, we can attribute that partially to pulling an all-nighter. I definitely get dumber without sleep. But the real problem was that I just didn’t understand much of anything about web development. Plus, my Python was meh at best. The Django polls tutorial is good, but probably not where you want to start. First, learn Python I found that whole experience kind of frustrating. I made it through the whole app, but I didn’t know any programming and didn’t really understand how this web development framework even works. I didn’t understand any of the anatomy – the structure – of the code, so … -
To •••• With Passwords
Slides Video Notes THREAD: some marginalia and further reading for folks who attended my #nbpy talk and would like to explore further 👇🏻 — jacobian (@jacobian) November 2, 2019 -
Functions as Tables in Django and Postgres
Postgres has some excellent features. One of these is [set-returning functions](https://www.postgresql.org/docs/current/functions-srf.html). It's possible to have a function (written in SQL, or some other language) that returns a set of values. For instance, the in-built function `generate_series()` returns a set of values: {% highlight sql %} SELECT day::DATE FROM generate_series(now(), now() + INTERVAL '1 month', '1 day') day; {% endhighlight %} This uses a set returning function as a table source: in this case a single column table. You can use scalar set-returning functions from within Django relatively easily: [I blogged about it last year](https://schinckel.net/2018/11/02/set-returning-and-row-accepting-functions-in-django-and-postgres/). It is possible to [create your own set-returning functions](https://www.postgresql.org/docs/current/sql-createfunction.html). Further, the return type can be a `SETOF` any table type in your database, or even a "new" table. {% highlight postgresql %} CREATE OR REPLACE FUNCTION foo(INTEGER, INTEGER) RETURNS TABLE(id INTEGER, bar_id INTEGER, baz JSON[]) AS $$ SELECT foo.id AS id, bar.id AS bar_id, ARRAY_AGG(JSON_BUILD_OBJECT(bar.x, foo.y)) FROM foo INNER JOIN bar ON (foo.id = bar.foo_id) WHERE foo.y = $1 AND bar.x > $2 GROUP BY foo.id, bar.id $$ LANGUAGE SQL STABLE; {% endhighlight %} It's possible to have a Postgres VIEW as the data source for a Django model (you just set the `Meta.db_table` on the … -
Django Slug Tutorial
How to add slugs to a Django DetailView. -
How Django Experts Think
Carlton’s Python bugPython 3 Standard LibraryBest Python booksDjango for ProfessionalsDreyfus Model of Skill AcquisitionZen Mind, Beginner’s Mind book -
How to deploy Django project to Dokku
In this Dokku tutorial, I will talk about how to deploy Django project with Dokku, it would use Postgres db and Amazon S3 to store data and media files. -
How to make Django request.is_ajax() work with JS fetch()
Django's request object has a nifty little method is_ajax. It allows determining whether a request comes from a JS framework (aka old-school ajax). While it works fine with some JS libraries, including the venerable jQuery, it won't work out of the box with modern JS native fetch API ... Read now -
Detecting queries in Django tests
Putting this here so I can find it next time I need to know it... Django has a useful test assertion you can use to ensure you make a set number of queries. However, at times this is a bit less useful than it needs to be, because something changes and we do indeed have a different number of queries, but it's got nothing to do with the actual code under test. If you are running in `DEBUG=True` mode, then you can examine the queries that have been made to the database connection, and ensure the raw SQL of a specific query matches (and is not duplicated, for instance). This does require a little bit of trickery: {% highlight python %} from django.db import connection from django.test import TestCase, override_settings from foo.factories import FooFactory class TestTheThing(TestCase): def test_no_update_query(self): foo = FooFactory() # Our Foo instance should be smart enough to notice that nothing # has changed, and thus should not emit an UPDATE query. with override_settings(DEBUG=True): foo.save() self.assertFalse([ x for x in connection.queries if 'UPDATE' in x['sql'] ]) {% endhighlight %} This is a bit of a contrived case: in my case today it was a celery task that only … -
Things I want to remember about SSH
SSH, short for Secure Shell, is a protocol for secure network communications. It is widely used for executing commands on remote servers, and for file uploads or downloads. If you are working with Django, use Git version control, or administrate servers, you surely are using SSH. In this post, I want to share some technical details about it. Secure Shell is using private and public key pairs. You can either use automatically generated private and public keys combined with a password, or manually generated private and public keys. In the latter case, you need to keep your private key on your computer and upload the public key to the remote server. Creating a pair of SSH keys manually If you are using GitHub, Bitbucket, DigitalOcean, or some other service, you might have seen the possibility to upload public SSH keys for direct access to remote servers. Here is how you usually create the SSH keys on the computer from which you want to establish a secure connection (your local machine or one of your servers that has access to other servers or services). In the Terminal you would execute these commands: $ ssh-keygen $ ssh-agent /usr/local/bin/bash $ ssh-add ~/.ssh/id_rsa The … -
Things I want to remember about SSH
SSH, short for Secure Shell, is a protocol for secure network communications. It is widely used for executing commands on remote servers, and for file uploads or downloads. If you are working with Django, use Git version control, or administrate servers, you surely are using SSH. In this post, I want to share some technical details about it. Secure Shell is using private and public key pairs. You can either use automatically generated private and public keys combined with a password, or manually generated private and public keys. In the latter case, you need to keep your private key on your computer and upload the public key to the remote server. Creating a pair of SSH keys manually If you are using GitHub, Bitbucket, DigitalOcean, or some other service, you might have seen the possibility to upload public SSH keys for direct access to remote servers. Here is how you usually create the SSH keys on the computer from which you want to establish a secure connection (your local machine or one of your servers that has access to other servers or services). In the Terminal you would execute these commands: $ ssh-keygen$ ssh-agent /usr/local/bin/bash$ ssh-add ~/.ssh/id_rsa The id_rsa is … -
Middleware
Weekly Django Chat newsletterMiddleware in Django DocsCarlton's DjangoCon US 2019 talk on Django as a MicroframeworkWSGIBaseHandler class in Django source code -
Testing Cloudflare workers
I like Cloudflare workers. They allow you to execute arbitrary Javascript as close to your users as possible. For simple use cases your scripts probably don't need to be tested, but once they grow in size and complexity you probably want to be confident that they don't break production. Something I dislike about the serverless trend is that most platforms are hard to test and debug. Testing is poorly documented and examples are far and few between. I wrote this blog post after implementing a non-trivial Cloudflare worker script for a client. The script used the Caching API, communicated with a backend server and manipulated HTTP headers. It required correctness tests before we rolled it out in production. I dug through the Cloudflare documentation and related blog posts to find testing examples, but there wasn't enough material out there so I figured I'd consolidate the knowledge I have on testing Cloudflare workers. The starting point for testing workers is in the [Cloudflare](https://developers.cloudflare.com/workers/archive/deploying-workers/unit-testing/) documentation. However I struggled get the examples to work and it didn't cover common Node.js testing techniques such as stubbing, mocking and running debuggers. This post will cover common testing techniques and scenarios I use to test Cloudflare … -
Testing Cloudflare workers
I like Cloudflare workers. They allow you to execute arbitrary Javascript as close to your users as possible. For simple use cases your scripts probably don't need to be tested, but once they grow in size and complexity you probably want to be confident that they don't break production. Something I dislike about the serverless trend is that most platforms are hard to test and debug. Testing is poorly documented and examples are far and few between. I wrote this blog post after implementing a non-trivial Cloudflare worker script for a client. The script used the Caching API, communicated with a backend server and manipulated HTTP headers. It required correctness tests before we rolled it out in production. I dug through the Cloudflare documentation and related blog posts to find testing examples, but there wasn't enough material out there so I figured I'd consolidate the knowledge I have on testing Cloudflare workers. The starting point for testing workers is in the [Cloudflare](https://developers.cloudflare.com/workers/archive/deploying-workers/unit-testing/) documentation. However I struggled get the examples to work and it didn't cover common Node.js testing techniques such as stubbing, mocking and running debuggers. This post will cover common testing techniques and scenarios I use to test Cloudflare … -
Signals
Signals in the Django docs -
Migrating from Python 2 to 3 at EdX - David Ormsbee & Nimisha Asthagiri
EdX WebsiteEdX Github Repodjango-waffleOpen edX Proposals -
Multipage Forms in Django
Introduction Most online forms fit on a single page. Think of a "join our forum" or "contact us" form into which the user enters a name, email address, and maybe a few other pieces of information. If you're building this kind of functionality into a Django site, you can take advantage of Django's built-in form classes. These are especially handy when dealing with model forms, where the form fields correspond to the fields on a model that will be recorded in your database. But what if you need a form that spans more than one page? Like a multipage job application where your personal details are on page 1, your relevant experience is on page 2, and so on? There are third-party libraries to help with this, but if you do it yourself you can sidestep a dependency and maybe become more familiar with Django's form handling. So let's do that. In the post below we'll go step-by-step through the creation of a multipage job application form. We'll start with the simplest functionality and then make it (slightly) more sophisticated. The most important modules ("models.py", "forms.py", and "views.py") will be reproduced here, but a working, standalone project is available from …