Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Configure Django to log exceptions in production
Django default logging behaviour for unhandled exceptions is: With DEBUG=True (Development) Log the exception on console/stream. Show the exception on page, i.e in http response. With DEBUG=False (Production) Do not log the exception on console/stream. Do not show the exception in response. Send an email to admin if email settings are configured correctly. Usually not logging the exception on console isn't a problem since an exception email is sent to you which can help you know the source of exception. But this assumes that email settings are configured correctly else you will not receive the exception email. You might not have email settings configured correctly and don't want to get into that right away. You might instead want to log the exception on console even with DEBUG=False. This post would help you in such scenario. Default logging configuration Django's default logging setting is: DEFAULT_LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', }, 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', }, }, 'formatters': { 'django.server': { '()': 'django.utils.log.ServerFormatter', 'format': '[%(server_time)s] %(message)s', } }, 'handlers': { 'console': { 'level': 'INFO', 'filters': ['require_debug_true'], 'class': 'logging.StreamHandler', }, 'django.server': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'formatter': 'django.server', }, 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], … -
Building a CRUD Application with Django Rest Framework and Vue.js
In this tutorial, you will learn how to use Django and Vue.js to build a modern CRUD (Create, read, update and delete operations are essential for the majority of web applications) web application. You'll also learn how to integrate Django Rest Framework with Vue.js and how to make HTTP calls using vue-resource (you can also use Axios or the browser's fetch API). In nutshell, Django is a back-end framework for building web applications with Python. Vue.js is a user interface library for creating JavaScript applications in the front-end. Django Rest Framework is a Django module to create Rest-based APIs that can be then consumed from browsers or mobile devices. You can use any database management system such as MySQL or PostgreSQL etc. Since the Django ORM can abstracts away all the differences between database systems and let work with any database without writing new code. The integration part simply consists of using an HTTP client like Axios, vue-resource or even better the browser's fetch API to call APIs exposed by DRF from the Vue.js application. Both server and client applications are decoupled so you can swap any part with any other library in the future. You can also create mobile … -
Building a CRUD Application with Django Rest Framework and Vue.js
In this tutorial, you will learn how to use Django and Vue.js to build a modern CRUD (Create, read, update and delete operations are essential for the majority of web applications) web application. You'll also learn how to integrate Django Rest Framework with Vue.js and how to make HTTP calls using vue-resource (you can also use Axios or the browser's fetch API). In nutshell, Django is a back-end framework for building web applications with Python. Vue.js is a user interface library for creating JavaScript applications in the front-end. Django Rest Framework is a Django module to create Rest-based APIs that can be then consumed from browsers or mobile devices. You can use any database management system such as MySQL or PostgreSQL etc. Since the Django ORM can abstracts away all the differences between database systems and let work with any database without writing new code. The integration part simply consists of using an HTTP client like Axios, vue-resource or even better the browser's fetch API to call APIs exposed by DRF from the Vue.js application. Both server and client applications are decoupled so you can swap any part with any other library in the future. You can also create mobile … -
Django Logging, The Right Way
Good logging is critical to debugging and troubleshooting problems. Not only is it helpful in local development, but in production it's indispensable. When reviewing logs for an issue, it's rare to hear somebody say, "We have too much logging in our app." but common to hear the converse. So, with that in mind, let's get started. A Crash Course in Python Loggers At the top of every file, you should have something like this: import logging logger = logging.getLogger(__name__) __name__ will evaluate to the dotted Python path of the module, e.g. myproject/myapp/views.py will use myproject.myapp.views. Now we can use that logger throughout the file like so: # A simple string logged at the "warning" level logger.warning("Your log message is here") # A string with a variable at the "info" level logger.info("The value of var is %s", var) # Logging the traceback for a caught exception try: function_that_might_raise_index_error() except IndexError: # equivalent to logger.error(msg, exc_info=True) logger.exception("Something bad happened") Note: Python's loggers will handle inserting variables into your log message if you pass them as arguments in the logging function. If the log does not need to be output, the variable substitution won't ever occur, helping avoid a small performance hit for … -
Django Logging, The Right Way
Good logging is critical to debugging and troubleshooting problems. Not only is it helpful in local development, but in production it's indispensable. When reviewing logs for an issue, it's rare to hear somebody say, "We have too much logging in our app." but common to hear the converse. So, with that in mind, let's get started. A Crash Course in Python Loggers At the top of every file, you should have something like this: python import logging logger = logging.getLogger(__name__) __name__ will evaluate to the dotted Python path of the module, e.g. myproject/myapp/views.py will use myproject.myapp.views. Now we can use that logger throughout the file like so: python # A simple string logged at the "warning" level logger.warning("Your log message is here") # A string with a variable at the "info" level logger.info("The value of var is %s", var) # Logging the traceback for a caught exception try: function_that_might_raise_index_error() except IndexError: # equivalent to logger.error(msg, exc_info=True) logger.exception("Something bad happened") Note: Python's loggers will handle inserting variables into your log message if you pass them as arguments in the logging function. If the log does not need to be output, the variable substitution won't ever occur, helping avoid a small performance … -
Install OpenCV 3 for Python on Mac
<div class='alert alert-warnin... -
Django Tips & Tricks #9 - Auto Register Models In Admin
Inbuilt admin interface is one the most powerful & popular feature of Django. Once we create the models, we need to register them with admin, so that it can read metadata and populate interface for it. If the django project has too many models or if it has a legacy database, then adding all those models to admin becomes a tedious task. To automate this process, we can programatically fetch all the models in the project and register them with admin. from django.apps import apps models = apps.get_models() for model in models: admin.site.register(model) This works well if we are just auto registering all the models. However if we try some customisations and try to register them in admin.py files in our apps, there will be conflicts as Django doesn't allow registering the same model twice. So, we need to make sure this piece of code runs after all admin.py files are loaded and it should ignore models which are already registered. We can safely hook this code in appconfig. from django.apps import apps, AppConfig from django.contrib import admin class CustomApp(AppConfig): name = 'foo' def ready(self): models = apps.get_models() for model in models: try: admin.site.register(model) except admin.sites.AlreadyRegistered: pass Now all models … -
Django Template language Intro
Django follows the MVC (Model = database-logic, View=business-logic Controller=presentation logic ). We can write the required logics based on programming[python] syntax in models and views but, when we want to write simple logics we should follow the django's template syntax, because programming[python] syntax is not allowed. We have the Template syntax as follows: Syntax Explanation Example {{ variable name }} To display the variable value. Just like " print " as in python input: Welcome "{{ request.user.username }}" output: Welcome "Abhi Ram" {% if condition %} --- logic ---- {% endif %} It is a conditional expression same as " if " condition in python input: {% if request.user.can_edit %} <button> Edit </button> {% endif %} output: if user has attribute "can_edit" as True then it will give result " <button> Edit </button> " nothing otherwise. {% if condition1 %} --logic-- {% elif condition2 %} -- logic-- {% else %} -- logic -- {% endif %} same as python's " if...elif....else " just like above {% for iterable object %} --logic-- {% endfor %} takes an iterable object returns an iterator object in every iteration. To … -
Large scale search for social sciences - Wouter van Atteveldt
(Summary of a talk at a Dutch Python meetup). Full title: large scale search and text analysis with Python, Elastic, Celery and a bit of R. Wouter teaches political sciences at the university and uses a lot of text analysis. He showed an example of text analysis of the Gaza-Isreal conflict, comparing USA media talking about Isreal and Chinese media talking about Israel. You saw big differences! The USA media talks more about rocket attacks by Hamas on Israel. Chinese media talks more about the results on the ground: invasion, deaths, etc. Text analysis is very important for social sciences. There's a flood of digital information, online and archived. Major newspapers in the Netherlands have been digitalized by the royal library, for instance. Lots of text to work with. You see the same with facebook and so: they can extract lots of info from the texts people type in! Facebook once did an experiment on positive/negative tweaks to timelines. Totally bullshit from a scientific viewpoint. So we cannot leave social research to the likes of facebook. So.... there is a need for a good tool, especially for education. They build it in python. Why python? Open source, platform independent, relatively … -
Building robust commandline tools with click and flask - Wojtek Burakiewicz
(Summary of a talk at a Dutch Python meetup). Wojtek likes simple tools. His presentation is about a recent tool they build: an internal tool that deploys python applications to servers with virtualenvs. It also configures nginx. They based the architecture around microservices. So you basically write a simple script that talks to a couple of APIs that in turn talk to other APIs. They started out with stash, artifactory, ansible, jenkins, supervisord and JIRA. All except JIRA have a nice API. Their deploy tool talked to those APIs. One problem was authentication. One tool needs user/pass, the other an auth token, the next an ssh key... Another problem was network segmentation. You don't want every laptop to talk to your production environment... The solution was to use one intermediary API. So the command line tool talks to the intermediary API which in turns talks to the APIs mentioned above. Another advantage of an intermediary API is that you can unify the concepts. You can just talk about "host" and "application", even though in Jenkins/ansible/etc it might be a "job" or a "machine. You can also exchange components! You can switch from Stash to Bitbucket without the user of the … -
Extract GPS & Exif Data from Images using Python
Smartphones & DSLRS attach all... -
Securing Django Application with multi factor authentication using Django-MFA
What is MFA? Multifactor authentication (MFA) is a security system that requires more than one method of authentication to verify the user’s identity for a login or other transaction. Why go for MFA? One of the major problems with traditional user ID and password login is the need to maintain a password database. Whether encrypted or not, if the database is captured it provides the hacker with a source to verify his guesses at speeds limited only by his hardware resources. Given enough time, a captured password database will fall. To avoid this break we do prefer multifactor authentication. Multifactor Authentication Technologies: There are multiple ways we could get the MFA like using hardware devices that the user carries to authorize access to a network service. Software-based security token applications that generate a single-use login PIN. Soft tokens are often used for multifactor mobile authentication, in which the device itself – such as a smartphone – provides the possession factor or SMS messages and phone calls sent to a user as an out-of-band method, smartphone OTP apps. In the current blog post, we see how to implement MFA in Django. How can we implement MFA in Django: We do have … -
How to monitor django application live events with sentry
In the previous tutorial you saw how to setup sentry, let us now learn how to track exceptions and events in SENTRY. We will setup client and server end tracking for Django Project. Note: My domain is set as "http://sentry.domain.com". Now after first login the screen should be similar to this. As per Sentry's Internal Architecture, Team will have access to projects. Create a team and assign a project to team, every member in team will now have access to it. As per your convineance. any combination of members can grouped as teams (which can be managed in Team Settings option in Dashboard page). Creating Team and Project Click on Create Team button in Dashboard. decide a Team Name and save changes create a Project. In Project DashBoard> Settings > Client Keys, you can find DSN (reffered in this tutorial as dsn) and DSN (Public) - (Reffered in this tutorial as public-dsn) Modifications for Django Application The recieving end is sentry but error tracker and logger is RAVEN, Install raven in Django Project environment pip install raven open settings.py include the below lines in settings.py RAVEN_CONFIG = { 'dsn': '<your-dsn-here>', } LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'root': { 'level': 'WARNING', 'handlers': ['sentry'], }, 'formatters': { 'verbose': … -
Python Hashes and Equality
Most Python programmers don’t spend a lot of time thinking about how equality and hashing works. It usually just works. However there’s quite a bit of gotchas and edge cases that can lead to subtle and frustrating bugs once one starts to customize their behavior – especially if the rules on how they interact aren’t understood. -
Django Uygulamalarımızı uWSGI ve Nginx ile Google Cloud Platformdan nasıl yayınlarız? 3. Bölüm
Artık bir sanal sunucumuz, gerçek bir IP adresimiz var ve Django projelerini bu sanal sunucumuzda çalıştırabiliyoruz. Şimdi uWSGI kurulumuna geldi. uWSGI, WSGI adı verilen standart bir arabirim üzerinden uygulamalarla iletişim kurabilen bir uygulama sunucudur. İnternette birçok örnekte uWSGI python sanal ortamda kuruluyor. Biz uWSGI’yi global olarak kuracağız. Bu, birden fazla Django projesinin işletilmesinde daha az sürtüşme oluşturacaktır. uWSGI’yi kurarken ve konfigüre ederken biraz heyecan duymalısınız. Eğer heyecanlanmıyorsanız işler yolunda gitmeyecek ve kısa bir süre sonra yaşadığınız olumsuzluklar sizi bu işten soğutacaktır.sudo apt-get update sudo apt-get install python3-devsudo -H pip3 install uwsgiBir önceki bölümden 8000 nolu portta çalışan python server’imiz olabilir. Eğer hala çalışıyorsa kapatalım.sudo lsof -t -i tcp:8000 | xargs kill -9kontrol edelim. uwsgi --http 0.0.0.0:8000 --home /home/zafercengiz/myapp/myvenv --chdir /home/zafercengiz/myapp/myapp -w myapp.wsgi Burada, uWSGI’ye, /home/zafercengiz/myapp/myvenv dizininde bulunan sanal ortamımızı, /home/zafercengiz/myapp/myapp dizininde bulunan wsgi.py dosyasını kullanarak 8000 numaralı bağlantı noktasından HTTP ye sunmasını söyledik. Artık uWSGI yi konfigüre edebiliriz. Çünkü hala http://benimsitem.com adresinden yayınlamaya hazır değiliz. crlt-C ye basarak uWSGI ekranından çıkalım. … bu bölüm devam edecek. -
Django Uygulamalarımızı uWSGI ve Nginx ile Google Cloud Platformdan nasıl yayınlarız? 2. Bölüm
Google Cloud Platformda bir sanal sunucu oluşturmuş, üzerine nginx kurmuş ve “Welcome to nginx” demiştik. Şimdi startproject bir django uygulamasını sanal sunucumuza yükleyip, IP adresimiz üzerinden yayınlayacağız.sudo apt-get updatebunu alışkanlık haline getirecektir. pip yüklüyoruz.sudo apt-get install python3-pipSanal ortamımızsudo apt-get install python3-venvzafercengiz@myapp:~$ pwd /home/zafercengiz zafercengiz@myapp:~$ mkdir myapp zafercengiz@myapp:~$ cd myappOluşturduğumuz dizinin altında sanal ortamınızı atalım.python3 -m venv myvenv source myvenv/bin/activate# böyle birşey görmeliyiz. (myvenv) zafercengiz@myapp:~/myapp$İlk startproject uygulamamızı kuralım. Tabiki ilk önce django yüklemeliyiz.pip install django django-admin startproject myappTüm bunları bildiğinizi varsayıyorum. Bunları kendi makinenizde bol bol uygulamışsınızdır../manage.py runserver Şu an için uygulamamız sadece local adresimizde çalışıyor. Uygulamamızı tüm dünyanın görmesi için Google Cloud Platform’da bazı güvenlik ayarlarını yapmamız gerekiyor. Google Cloud Platform ana sayfasına gidip ilgili linki tıklıyoruz. O linkin resmi aşağıda. Açılan sayfadan GÜVENLİK DUVARI KURALI OLUŞTUR u tıklıyoruz. Buradakinin aynısını yapın. Burada özetle şunu yaptık. 8000 numaralı portu TCP altında dışardan erişime açtık. Herşey iyi gidiyor. Birkaç düzenlemeye daha ihtiyaç var. Uygulamamızın settings.py dosyasına gidip ALLOWED_HOSTS = [] ayarını yapmalıyız. Umarım buradaki IP adresinin ne olduğunu unutmamışsınızdır. Bütün başlangıç noktamız zaten bu IP adresiydi. Bolum 1 link. (myvenv) zafercengiz@myapp:~/myapp/myapp$ ./manage.py runserver 0.0.0.0:8000Bir hatırlatma ve ipucu. ./manage.py runserver komutunu sık kullandığımız için bazen sunucudan düşmeyebiliriz. Yani port açık kapabilir. Bu … -
Django Tips & Tricks #8 - Hyperlink Foreignkey Fields In Admin
Consider Book model which has Author as foreignkey. from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author) We can register these models with admin interface as follows. from django.contrib import admin from .models import Author, Book class BookAdmin(admin.ModelAdmin): list_display = ('name', 'author', ) admin.site.register(Author) admin.site.register(Book, BookAdmin) Once they are registed, admin page shows Book model like this. While browing books, to go to a particular author, we have to previous page, go to Author model and then find relevant author. This becomes tedious if we spend lot of time in admin. Instead, if author field has a hyperlink, we can directly go to its page. Django provides an option to access admin views by its URL reversing system. For example, we can get add view of author model in book app from reverse("admin:book_author_add"). To hyperlink author field in book admin, get url from reversing book_author_change with its id and return required html. class BookAdmin(admin.ModelAdmin): list_display = ('name', 'author_link', ) def author_link(self, book): link = reverse("admin:book_author_change", args=[book.author.id]) return u'<a href="%s">%s</a>' % (link, book.author.name) author_link.allow_tags = True author_link.short_description = 'Author' Now in the book admin view, author field will be hyperlinked and we can … -
Django Uygulamalarımızı uWSGI ve Nginx ile Google Cloud Platformdan nasıl yayınlarız? 1. Bölüm
Giriş Django, python uygulamalarımızı ve web sitelerimizi web sunuculardan yayınlamamızı sağlayan güçlü bir web framework’tur. Django ürettiğimiz kodları yerel olarak sınayabileceğimiz bir yerel sunucuyla birlikte gelir. Web sitemizi gerçek sunuculardan yayınlama esnasında bu yerel sunucu basit kalır ve güvenli değildir. Bu klavuzda Django uygulamalarımızı yayınlamak için Google Cloud Platformu kullanacağız. Bölüm’de Google Cloud Platform üzerinde bir sanal sunucu kurarak Nginx yükleyeceğiz ve makinemizi Django uygulamalarının çalışmasına hazır hale getireceğiz. Bölüm’de sunucumuzda startproject bir Django uygulaması kuracağız ve bu uygulamayı IP adresimizden yayınlayacağız. (Tüm dünya görecek) Bölüm’de uygulamamızı uWSGI yapılandırarak bir domain altında, Nginx üzerinden yayınlayacağız. (www.benimsitem.com gibi. Bölüm’de kendi makinemizde bulunan django uygulamamızı GIT deposuna atarak, sanal sunucumuz ile bu GIT deposuna bağlantı kuracak ve bir domain üzerinden uygulamamızı yayınlayacağız. Başlıyoruz… İlk işimiz Google Cloud Platformda ücretsiz hesabımızı oluşturmak. Google ilk hesap açılışlarında 300 USD bir kredi veriyor. Google hesabınızın olduğunu ve Google Cloud Platforma giriş yaptığınızı varsayıyorum. İlk işimiz Google Cloud Platformda ücretsiz hesabımızı oluşturmak. Google ilk hesap açılışlarında 300 USD bir kredi veriyor. Google hesabınızın olduğunu ve Google Cloud Platforma giriş yaptığınızı varsayıyorum. Sanal Makine Oluşturma Oluşturu tıklayıp bekliyoruz. Bu işlem bazen 10 dakika kadar sürebiliyor. Endişe etmeyin ve bekleyin. Herşey yolunda giderse karşımıza ilk makinemiz geliyor. Ben … -
How To Set Field-Level Error From Serializer Validate() Method In Django Rest Framework
Ouch, it looks like it's the longest post title of this blog! But the solution itself is pretty short and straightforward. When you raise a serializers.ValidationError, you may be tempted to provide an error as a string argument. Which works, but as a result, this error will be ... Read now -
My experience of mentoring at Django Girls Bangalore 2017
TL;DR Last Sunday, Django Girls Bangalore organized a hands-on session of web programming. This is a small event report from my side. Detailed overview Django Girls is not for a profit initiative led by Ola Sitarska and Ola Sendecka. Such movement helps women to learn the skills of website development using the well-known web-framework Django. This community is backed by organizers from many countries. Organizations like The Python Software Foundation, Github, DjangoProject and many more are funding Django Girls. Django Girls Bangalore chapter was organized by Sourav Singh and Kumar Anirudha. This was my second time mentoring for the Django Girls event. First was for the Ahmedabad chapter. The venue was sponsored by HackerEarth. 8 male and 2 female mentored 21 women during this event. Each mentor was assigned more or less 3 participants. Introducing participants with web development becomes easy with the help of Django Girls handbook. The Django Girls handbook is a combination of beginner-friendly hands-on tutorials described in a simple language. The handbook contains tutorials on basics of Python programming language to deploying your web application. Pupils under me were already ready by pre-configuring Python with their workstation. We started by introducing our selves. We took some … -
PyRoma 2017
The last of the PyRoma (Python User Group of Rome, Italy) periodic meetings in 2017 -
A Django custom command to write model fields on an Excel file
Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code. Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need. You can use the following Django custom command: The option –exclude-models can be used to specify a text file with models to exclude from Excel file (one per line). The option –include-reverse-relations (default false) can be used to include also reverse relations of your models, that are fields that are not part of the models itself, but are reverse fields from other models that have for instance a ForeignKey to the model. The command depends on the XlsxWriter package to create the Excel file. -
A Django custom command to write model fields on an Excel file
Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code. Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need. You can use the following Django custom command: https://gist.github.com/baxeico/1a06f5491f175510356359421b22cdec You can run the command like this: ./manage.py excel_models app1 app2 models.xlsx And it will write all the models of app1 and app2 on the models.xlsx file. Every model will be written on a different sheet. The option –exclude-models can be used to specify a text file with models to exclude from Excel file (one per line). The option –include-reverse-relations (default false) can be used to include also reverse relations of your models, that are fields that are not part of the models itself, but are reverse fields from other models that have for instance a ForeignKey to the model. The command depends on the XlsxWriter package to create the Excel file. The post A Django custom command to write model fields on an Excel file appeared first on Augusto Destrero - Freelance developer and sysadmin. -
A Django custom command to write model fields on an Excel file
Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code. Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need. You can use the following Django custom command: # coding=utf-8 from __future__ import unicode_literals import logging from django.core.management.base import BaseCommand, CommandError from django.apps import apps from xlsxwriter.workbook import Workbook logger = logging.getLogger(__name__) class Command(BaseCommand): help = 'Export all models of a given app to Excel file' def add_arguments(self, parser): parser.add_argument('app_labels', nargs='+', type=str) parser.add_argument('output', type=str) parser.add_argument('--exclude-models', dest="exclude_models_file", type=file) parser.add_argument('--include-reverse-relations', action='store_true', dest="include_reverse_relations", default=False) def handle(self, *args, **options): workbook = Workbook(options['output'], {'constant_memory': True}) bold = workbook.add_format({'bold': True}) headers = ['Field', 'Type'] exclude_models = [] if options['exclude_models_file']: exclude_models = options['exclude_models_file'].read().splitlines() for app_label in options['app_labels']: app_config = apps.get_app_config(app_label) app_models = app_config.get_models(include_auto_created=False) for model in app_models: model_name = model.__name__ if model_name in exclude_models: continue worksheet_name = '%s - %s' % (app_label, model_name) if len(worksheet_name) > 31: worksheet_name = '%s - %s' % (app_label[:3], model_name) worksheet = workbook.add_worksheet(worksheet_name) worksheet.write_row(0, 0, headers, bold) worksheet.set_column(0, len(headers), 30) fields = model._meta.get_fields() row = 1 for f in fields: try: fieldname = f.verbose_name except: … -
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 ... Read now