Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Preventing Model Overwrites in Django and Postgres
I had an idea tonight while helping someone in [#django](http://irc.lc/freenode/django). It revolved around using a postgres trigger to prevent overwrites with stale data. Consider the following model: {% highlight python %} class Person(models.Model): first_name = models.TextField() last_name = models.TextField() {% endhighlight %} If we had two users attempting to update a given instance at around the same time, Django would fetch whatever it had in the database when they did the GET request to fetch the form, and display that to them. It would also use whatever they sent back to save the object. In that case, the last update wins. Sometimes, this is what is required, but it does mean that one user's changes would be completely overwritten, even if they had only changed something that the subsequent user did not change. There are a couple of solutions to this problem. One is to use something like [django-model-utils](https://django-model-utils.readthedocs.io) [FieldTracker](https://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker) to record which fields have been changed, and only write those back using [`instance.save(update_fields=...)`](https://docs.djangoproject.com/en/3.1/ref/models/instances/#specifying-which-fields-to-save). If you are using a django Form (and you probably should be), then you can also inspect `form.changed_data` to see what fields have changed. However, that may not always be the best behaviour. Another solution would … -
Preventing Model Overwrites in Django and Postgres
I had an idea tonight while helping someone in [#django](http://irc.lc/freenode/django). It revolved around using a postgres trigger to prevent overwrites with stale data. Consider the following model: {% highlight python %} class Person(models.Model): first_name = models.TextField() last_name = models.TextField() {% endhighlight %} If we had two users attempting to update a given instance at around the same time, Django would fetch whatever it had in the database when they did the GET request to fetch the form, and display that to them. It would also use whatever they sent back to save the object. In that case, the last update wins. Sometimes, this is what is required, but it does mean that one user's changes would be completely overwritten, even if they had only changed something that the subsequent user did not change. There are a couple of solutions to this problem. One is to use something like [django-model-utils](https://django-model-utils.readthedocs.io) [FieldTracker](https://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker) to record which fields have been changed, and only write those back using [`instance.save(update_fields=...)`](https://docs.djangoproject.com/en/3.1/ref/models/instances/#specifying-which-fields-to-save). If you are using a django Form (and you probably should be), then you can also inspect `form.changed_data` to see what fields have changed. However, that may not always be the best behaviour. Another solution would … -
Using sorl-thumbnail with Redis on Heroku
I recently added sorl-thumbnail to a project for creating smaller image files. I chose to configure with a Redis Key-Value Store, as I’ve heard Redis is super hip,… -
Deep dive: Django Q and SQS
When working on a Django application the de facto recommendation for a task queue is Celery. I believe this is a good recommendation. It is kind of like buying IBM – “no one was ever fired for buying IBM”. I started using Django Q more recently and it is doing a great job. One system I built using it processes roughly 400k tasks per day. Surely not the largest system and surely not the most impressive number, but decent enough to say that Django Q is a solid choice. But as with many smaller projects there are sometimes a few gotchas you are running into. This becomes painfully obvious when setting up an app using SQS. Let me walk you through the steps I took to make Django Q play nicely with our AWS setup at Grove Collaborative. Redis is great, but… First of all you have to configure Django Q to use SQS. You do this by adding the Q_CLUSTER dictionary to your settings.py with the sqs key. If you are familiar with AWS and boto3 you might know that you can either provide the AWS region when initialising a new connection or you can have a standard config … -
Deep dive: Django Q and SQS
When working on a Django application the de facto recommendation for a task queue is Celery. I believe this is a good recommendation. It is kind of like buying IBM - “no one was ever fired for buying IBM”. I started using Django Q more recently and it is doing a great job. One system I built using it processes roughly 400k tasks per day. Surely not the largest system and surely not the most impressive number, but decent enough to say that Django Q is a solid choice. But as with many smaller projects there are sometimes a few gotchas you are running into. This becomes painfully obvious when setting up an app using SQS. Let me walk you through the steps I took to make Django Q play nicely with our AWS setup at Grove Collaborative. Redis is great, but… First of all you have to configure Django Q to use SQS. You do this by adding the Q_CLUSTER dictionary to your settings.py with the sqs key. If you are familiar with AWS and boto3 you might know that you can either provide the AWS region when initialising a new connection or you can have a standard config … -
Using sorl-thumbnail with Redis on Heroku
I recently added sorl-thumbnail to a project for creating smaller image files. I chose to configure with a Redis Key-Value Store, as I’ve heard Redis is super hip, but ran into trouble during deployment. In this post, I’ll document how I set up Redis locally with Docker, then in production on Heroku. Local Docker Setup The logical place to start is with the sorl-thumbnail documentation. Install the package python3 -m pip install sorl-thumbnail Add package to settings.py # settings.py INSTALLED_APPS += ['sorl.thumbnail'] Migrate database python manage.py migrate This should create a model called KVStore. Add redis server If we navigate to the documentation’s requirements page, we’ll see we need some sort of key-value store and image processing library. As the title suggests, we’re going to use Redis. Let’s add that in our docker-compose.yml file. # docker-compose.yml version: "3.7" services: web: build: ./django command: python /code/manage.py runserver 0.0.0.0:8000 env_file: - ./.env.dev volumes: - ./django:/code ports: - 8000:8000 - 443:443 depends_on: - db - redis db: image: postgres volumes: - postgres_data:/var/lib/postgresql environment: - POSTGRES_HOST_AUTH_METHOD=trust - POSTGRES_USER=username - POSTGRES_PASS=supercomplexpasswordthatishardtocrack - POSTGRES_DBNAME=pg ports: - "5432:5432" redis: restart: always image: redis:latest ports: - "6379:6379" volumes: - redis_data:/var/lib/redis volumes: postgres_data: redis_data: networks: default: I’m assuming here … -
Anatomy Of An Application
In the previous Understand Django article, we got deep into the Django administrators site. We saw what the site was and how to configure and customize it. In this article, we will examine what goes into an application. Applications are core elements of a Django project. From Browser To DjangoURLs Lead The WayViews On ViewsTemplates For User InterfacesUser Interaction With FormsStore Data With ModelsAdminister All The ThingsAnatomy Of An Application What Is An Application? -
Deep dive: Django Q and SQS
Deep dive: Django Q and SQS When working on a Django application the de facto recommendation for a task queue is Celery. I believe this is a good recommendation. It is kind of like buying IBM - “no one was ever fired for buying IBM”. I started using Django Q more recently and it is doing a great job. One system I built using it processes roughly 400k tasks per day. Surely not the largest system and surely not the most impressive number, but decent enough to say that Django Q is a solid choice. But as with many smaller projects there are sometimes a few gotchas you are running into. This becomes painfully obvious when setting up an app using SQS. Let me walk you through the steps I took to make Django Q play nicely with our AWS setup at Grove Collaborative. Redis is great, but… First of all you have to configure Django Q to use SQS. You do this by adding the Q_CLUSTER dictionary to your settings.py with the sqs key. If you are familiar with AWS and boto3 you might know that you can either provide the AWS region when initialising a new connection or … -
Django Best Practices: Security
Django is a mature, battle-tested web framework with a well deserved reputation for security over the past 15+ years.However the internet remains a dangerous place and web security is an … -
Embedding videos in feincms3
Embedding videos in feincms3 I have been using oEmbed services for about 10 years now to embed content from YouTube and Vimeo on other sites, first using feincms-oembed and later using feincms3.plugins.external. This worked well enough despite some problems such as Embed.ly introducing API keys and Noembed being more or less unmaintained since 2017. However, the requirement to fetch data from a different service always bothered me, especially since all I wanted (most of the time) was to generate a bare <iframe> containing the embed, nothing more. django-embed-video was almost what I needed but it had some worrysome thumbnail fetching code in there; also I didn’t understand the reason for defining backends, dynamically importing them etc. when all I wanted was a function where I would get back some HTML when passing a supported URL, or nothing if the URL wasn’t supported. Since I really like writing code1 here’s my solution to embedding YouTube and Vimeo videos as a part of feincms3, feincms3.embedding. Since it doesn’t depend on an external service (except the obvious ones) it is never gonna give you up if you just call: from feincms3.embedding import embed html = embed("https://www.youtube.com/watch?v=dQw4w9WgXcQ") Maybe it’s just a really strong NIH … -
Django Search Tutorial
__Note__: I gave a version of this tutorial at DjangoCon US 2019. You can see the video here: -
From PHP and SWISDK2 to Python and Django – 12 years later
From PHP and SWISDK2 to Python and Django – 12 years later Once at a time1 there was an agency founded by a few friends and myself. We were using PHP at the time and had our own framework, the SWISDK – Simple Web Infrastructure SDK; quite a mouthful. It supported many features which are now commonplace in web frameworks such as an ORM, a mostly autogenerated admin interface, forms which could be used together with models, some sort of routing, generic views2, XSS and CSRF protection, support for translations in the code and the database, images, galleries, comments etc. I think I had Django on my radar for quite a while already. I liked the template language and especially the template inheritance feature so much that I reimplemented it as an extension to the Smarty template engine. Smarty has built-in support for template inheritance since 2009 but I didn’t profit from this anymore, because we migrated to Python and Django in 2008 and have never looked back. This was after manipulators had been replaced by the newforms library (the current django.forms) but before the newforms admin and before Django 1.0. I also took a long and hard look at … -
Tweet from Django application using Tweepy
In this tutorial, we will learn how to post a tweet from Django application using Tweepy. -
<p>django-debug-toolbar 3.0 is now <a target="_blank" rel="nofollow" href="https://pypi.org/project/django-debug-toolbar/3.0/">available on PyPI</a>. Please help with testing or with success stories o
django-debug-toolbar 3.0 is now available on PyPI. Please help with testing or with success stories or with bug reporting & squashing. -
Django Authentication With GitHub
In this tutorial, we will build a Django application that allows users to sign in via their GitHub account. -
Django News - DjangoCon Europe starts soon. PyCon Africa talks now live. - Sep 17th 2020
News Python Software Foundation News: Noah Alorwu Awarded the PSF Community Service Award for Q2 2020 Noah Alorwu, software developer and co-founder of DjangoCon Africa has been awarded the Python Software Foundation 2020 Q2 Community Service Award. blogspot.com Pip: Buy a feature The Pip team wants your input on a survey to figure out what is most important to you. google.com Events DjangoCon Europe 2020 DjangoCon Europe kicks off September 18th. Tickets are still available for €29.00 and €79.00 for anyone who wants to support DjangoCon Europe and attend virtually! pretix.eu Articles r2c blog — Not all attacks are equal: understanding and preventing DoS in web applications From Django co-creator Jacob Kaplan-Moss, a guide to understanding and preventing Denial of Service (DoS) attacks. r2c.dev How to Unit Test a Django Management Command - Adam Johnson Management commands are an extremely useful feature in Django, but can be difficult to test. Adam Johnson demonstrates how to do so. adamj.eu Is Django a Full Stack Framework? An overview of back-end vs front-end frameworks and where Django sits in the mix. learndjango.com Sponsored Link Mystery Science Theatre 3000 with Your Friends Privately stream movies and chat with your friends on WeeVee. Read about … -
Running Django as a hidden service
Tor has many uses, especially for oppressed and persecuted people, people who have to fear for their safety when accessing information on the internet. The more people use Tor, the safer it gets. -
Administering Your App
Full show notes are available at https://www.mattlayman.com/django-riffs/8. -
feincms may still be relevant
feincms may still be relevant About 10 years ago there existed a few Django-based CMS. If someone didn’t already know which to choose, the following three systems were seen as reasonable choices3: Mezzanine. There were some doubts about Mezzanine’s maintenance status, see this Jazzband issue but they were unfounded. django CMS. Perhaps the best known Django-based CMS back then and maybe now too. feincms1 The landscape has changed. Wagtail hasn’t been the new kid on the block anymore for a long time. I have been following the development of those CMS more or less closely over the years and am convinced that all of them are good choices for starting new projects. We’re still using feincms at Feinheit; or more often django-content-editor and feincms3, especially for new projects. Since the development of the feincms 1.x line has slowed down to maintenance mode and the community of feincms3 is really small you might ask why we’re still sticking with those tools – we’re certainly asking it ourselves from time to time. Here’s a list of reasons why feincms is still relevant: There’s only one autogenerated administration interface to maintain and explain to users – the stock Django administration interface. It works … -
Regular Expressions in Python
Regular expressions, aka `rege... -
Angular 10 and Django 3 Image Files Upload with FormData
Throughout this tutorial, we'll see how we can implement file and image upload in Django 3, Django REST Framework and Angular 10 with a step by step example. Our application will expose an /upload REST API endpoint that accepts POST requests which contain the image file posted with a multipart/form-data content type via FormData. For the frontend, we'll be using Angular 10 to create a simple interface that allows the user to select a file or image and upload it to the server via a POST request using HttpClient and FormData. In the first section, we'll create the Django 3 REST API application and use a REST API client to test the upload endpoint. Next, in the second part, we'll proceed to create a frontend application with Angular 10 for uploading the image to the REST API server using HttpClient and FormData. Let's get started! Prerequisites For this tutorial, you will need to have a few prerequisites such as: Python and pip installed on your system. We'll be using Python 3.7, Familiarity with Python and Django. Node.js and NPM installed on your system. These are required by Angular CLI. Familiarity with TypeScript. Creating a Virtual Environment & Installing Django 3 … -
Django Community Survey
Django Community SurveyDjango PeoplePyCon AU 2020 playlistPyCon Africa 2020 playlistDjangoCon Europe 2020Django News newsletterWorking in Public by Nadia EghbalSupport the ShowOur podcast does not have a sponsor and is a labor of love. To support the show, please consider purchasing one of the books on LearnDjango.com or suggest one to a friend. -
Integrate Summernote Editor in Django application
In this tutorial, we will learn how to integrate Summernote WYSIWYG Editor in Django. -
Django x MongoDB
Here's a simple guide to using... -
Redis on Mac & Linux
Redis is a very popular data s...