Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django Crash Course Live!
This is your last chance to sign up for our class on October 2-4, 2020! Sign up at: Django Crash Course Live This is your chance to learn and interact live with Daniel and Fabio, two experienced Django developers! -
Deploying Django > 3.1 (async) with Uvicorn and Nginx
Django async Django 3.1 shipped with async views. This means we can start using async/await in Django views: async def index(request): res = await do_stuff_async() # return your view To reap all the benefits of this feature, you have to run Django under an ASGI server like Daphne, Uvicorn, or Hypercorn. In this guide I use Uvicorn. The setup The stack uses battle-tested components: PostgreSQL Nginx Uvicorn with Gunicorn Django async The setup Install and configure Postgres Install and configure Git, install Python Install Nginx Configure Nginx Install Certbot and configure SSL Preparing the environment for deploy Split settings Testing Django with Uvicorn Making Uvicorn persistent Static files, settings, and migrations Infinite redirects and SECURE_SSL_REDIRECT Automate all the things! Resources Install and configure Postgres First off, head over PostgreSQL Downloads and configure the appropriate repo for your operating system. Next up, install Postgres, start the service, and enable it at boot. Also, create a role and a database. Install and configure Git, install Python On the target production server install Git and Python > 3.6. Git is needed later for cloning and pulling the Python code. Install Nginx Head over the Nginx documentation and configure the appropriate repo for your … -
Django News - PyGotham TV, Django T-Shirts, and Many New Releases - Oct 1st 2020
News New DjangoCon t-shirts Beautiful new t-shirt designs representing DjangoCon US, Europe, Australia, and Africa have been added to the store! threadless.com Django 3.1.2 bugfix release A new bugfix package is available. djangoproject.com Django REST Framework 3.12 release The 3.12 release brings a handful of refinements to the OpenAPI schema generation, plus support for Django's new database-agnostic JSONField, and some improvements to the SearchFilter class. django-rest-framework.org Wagtail 2.10.2 release New updates to the popular Django CMS. github.com pytest 6.1.0 released A new version of the very popular Python testing package is out. pytest.org django-filter 2.4.0 released A security update to 2.4.0. Upgrading highly recommended. pypi.org Celery v5.0.0 Released Version 5.0.0 of Celery, a very popular distributed task queue. github.com Events PyGotham TV PyGotham TV is this Friday, Oct. 2 & Saturday, Oct. 3, and tickets are FREE. pygotham.tv Python Web Conf 2021 CFP Python Web Conference is a fully virtual, 3 day, 3 track conference you can join from the comfort of your home office. Their CFP just opened up. papercall.io Articles Understanding Django - Anatomy of an Application The 8th in a series from Matt Layman, this one focusing on understanding Django apps. mattlayman.com Deep dive: Django Q and … -
Check Web App Security With Bandit - Building SaaS #74
In this episode, I integrated the bandit static analysis tool to do automated security checking of my code before each commit. We talked about pre-commit and how to add in a new hook. After finishing that tool addition, we got deep into Django while removing some messages inserted by django-allauth on sign up. We began by talking about what the bandit tool does and how it works. Once I explained bandit, I focused on the bandit documentation to see how to add the tool. -
Teaching Python - Michael Kennedy
TalkPython.fmHow to Make a CNC Table Saw by Jeremy Fielding40% off all Talk Python Manning courses with the promo: poddjango20Manning | Talk Python: Python for Absolute BeginnersManning | Talk Python: MongoDB for DevelopersManning | Talk Python: Async Techniques and ExamplesManning | Talk Python: Building Data-Driven Web Apps with Flask and SQLAlchemySupport 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. -
How to Contribute to Django Docs
Makes updates to Django's official docs. -
Python, Django top frameworks and it's comparison
Python is a general-purpose programming language. We can use python for so many applications including web applications. Framework is a collection of modules or packages, which helps in writing a web application or helps in developing a web application. We don't have to worry about low level details such as protocols, socket, thread management when working with a framework. Framework makes the developer’s life smarter and easier by giving a structure for application development. They automate the implementation of common solutions, which gives flexibility to developers to focus only on app logic instead of routine. They provide common backend fast reliable and scalable easily maintainable web applications. Frameworks provides functionality to perform the following operations for developing web applications. URL Routing. Input form handling and validation. Output Formats with Templating Engine HTML, XML, JSON. Data Connection, manipulating using ORM mappers. Web security. Session Storage and retrieval. URL Routing Mechanism of URL mapping is staying at the same web page when you click on some other link. Input Form handling and Validation The data entered into a form needs to be in the right format, and certain fields are mandatory for effective use of form. Output Formats with Templating Engine Allows … -
Database Design Tutorial for Beginners
Databases are at the heart of every web application. Their design, or schema, is literally the blueprint for how all information is stored, updated, and accessed. However learning about databases … -
Why should you use Django for Web Development
Django is a highly powerful framework for building small and large scale web applications. Being a Python-based framework, Django for web development is the preferred choice of companies all over the world. If you are looking for proof of Django’s popularity, here it is – Django framework for web development is used by YouTube, Dropbox, […] The post Why should you use Django for Web Development appeared first on BoTree Technologies. -
Django News - Django Day Copenhagen 2020 - Sep 24th 2020
News Python 3.8.6 is now available Python 3.8.6 is a maintenance release. Check out the What's New In Python 3.8 document for more information about features. blogspot.com Django CMS 3.8.0 RC1 released The first Django CMS release under a newly founded Django CMS Association. This release focuses on Django 3.1 support. django-cms.org Events Django Day Copenhagen 2020 Djangonauts from in and around Denmark are meeting up for the second edition of Django Day, to be held on September 25th 2020. It will be a full day of talks, either to be experienced online -or- at our venue with safe social distancing. djangoday.dk Articles Django Best Practices: Security Django security best practices starting with the most important and working down the list. learndjango.com Docker Github Actions V2 Docker released V2 of their Docker Github Actions which adds some much better build and caching options. docker.com Django and its default values A dive into how Django's migration system handles default values on model fields. medium.com What is the Value of Browser Diversity? A thoughtful take on what's lost if one/two browsers dominate. daverupert.com feincms may still be relevant Thoughts on the Django CMS landscape from the creator of feincms. 406.ch Using sorl-thumbnail … -
How to use both camelCase and snake_case in frontend and backend
Python uses snake_case variable naming while JavaScript favours camelCase. When you're buiding an web API with Django then you'll be using both langauges together. How do you keep your styles consistent? You could just use one style for both your frontend and backend, but it looks ugly. Perhaps this is … -
Dynamically Regrouping QuerySets In Templates - Building SaaS #73
In this episode, we worked on a new view to display course resources. While building out the template, I used some template tags to dynamically regroup a queryset into a more useful data format for rendering. I started a new view before the stream to display content, but I had not filled it in before the stream started. We added new data to the context, and did some adjustments to the URL based on the required inputs for the view. -
Deep Dive into Flask's Application and Request Contexts
This post explores how the Application and Request contexts work in Flask. -
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: <iframe width="738" height="417" src="https://www.youtube.com/embed/is3R8d420D4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" … -
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 …