Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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 -
How To Get a List Of All User Permissions Available in Django Based Project
Django comes with a simple permissions system. It provides a way to assign permissions to specific users and groups of users. The system adds "change, remove and add" permissions automatically to every model class. And it's pretty easy to add custom permissions to a model class, like this: class ... Read now -
How To Show Correct List Item Indexes When Using Pagination in Django
In your Django template put something like this: <ul> {% for object in object_list %} <li>{{ forloop.counter0|add:page_obj.start_index }}. {{ object }}</li> {% endfor %} </ul> Where: object_list - is a list of objects produced by pagination; page_obj - is a page object produced by pagination page_obj ... Read now -
How To Exclude node_modules Directory When Running collectstatic Command in Django
If you use npm or yarn to install frontend packages inside your Django project, you may notice, that when you run python manage.py collectstatic command, it ends up collecting huge amounts of files. That's because by default, collectstatic grabs all content of static directories inside the project, including ... Read now -
Make Django Rest Framework and Axios Work Together Nicely
This is a solution to the problem I encountered while marrying Django Rest Framework powered API and Axios JS HTTP client: Axios issues GET requests with multi-value parameters in a bit different way than Django expects. When you create your API with Django Rest Framework, it expects multi-value GET parameters ... Read now -
Download AWS S3 Files using Python & Boto
<div class='alert-warning aler... -
Sorl-thumbnail to generate thumbnails in django
sorl-thumbnail is a very useful package to deal with images in the Django template. It is very easy to implement. Resizing and cropping images become simple with inbuilt tags provided by sorl-thumbnail. Installation and setup: To install sorl-thumbnail. pip install sorl-thumbnail To setup, add sorl.thumbnail to INSTALLED_APPS in settings. Then create the migrations with python manage.py makemigrations thumbnail and migrate with python manage.py migrate thumbnail Now sorl-thumbnail is ready to use in your project. To use sorl-thumbnail we should install python image library pip install Pillow Key Value Store: sorl-thumbnail needs key value store to maintain the keys and values of the thumbnails generated from the images and its storage. Storage can be our own server or it can be any cloud storage services like Amazon S3, Microsoft azure etc. To store the images in these cloud storage services, django-storages package is very useful. Template tags and filters for sorl-thumbnail: sorl-thumbnail has one tag and three filters to use in templates. To use these filters and tag we should load them first with {% load thumbnail %} thumbnail tag: When you use this tag sorl-thumbnail searches the thumbnail in Key Value … -
How to use django-cache-memoize
Last week I released django-memoize-function which is a library for Django developers to more conveniently use caching in function calls. This is a quick blog post to demonstrate that with an example. The verbose traditional way to do it Suppose you have a view function that takes in a request and returns a HttpResponse. Within, it does some expensive calculation that you know could be cached. Something like this: No caching def blog_post(request, slug): post = BlogPost.objects.get(slug=slug) related_posts = BlogPost.objects.exclude( id=post.id ).filter( # BlogPost.keywords is an ArrayField keywords__overlap=post.keywords ).order_by('-publish_date') context = { 'post': post, 'related_posts': related_posts, } return render(request, 'blogpost.html', context) So far so good. Perhaps you know that lookup of related posts is slowish and can be cached for at least one hour. So you add this: Caching from django.core.cache import cache def blog_post(request, slug): post = BlogPost.objects.get(slug=slug) cache_key = b'related_posts:{}'.format(post.id) related_posts = cache.get(cache_key) if related_posts is None: # was not cached related_posts = BlogPost.objects.exclude( id=post.id ).filter( # BlogPost.keywords is an ArrayField keywords__overlap=post.keywords ).order_by('-publish_date') cache.set(cache_key, related_posts, 60 * 60) context = { 'post': post, 'related_posts': related_posts, } return render(request, 'blogpost.html', context) Great progress. But now you want that cache to immediate reset as soon as the blog posts change. … -
You have two jobs
Welcome to FictionalSoft! I hope your first week is going well? Great. As you start to find your feet, I want to make sure we have a shared understanding of what success looks like here. Apologies in advance if I’m telling you something you already know, but it’s important to be explicit about this early. You were hired to write code. Many developers make the mistake and think that their job stops there. -
How to filter a Django Queryset using Extra
SQL Query in Django ORM: Using Django ORM we can perform all queryset operations. In some cases, we need to use SQL Queries in Django ORM. Here is the scenario if a model having a field with positiveInteger of another model, When I want to get an object of this positive Integer Field we will Query again for that model, By this number of Queries will be increased to reduce this we will use SQL Query to get appropriate results in single Query. testapp/models.py class State(models.Model): name=models.CharField(max_length=150) class City(models.Model): name=models.CharField(max_length=150) class Student(models.Model): name=models.CharField(max_length=150) state_id=models.PositiveIntegerField() city_id=models.PositiveIntegerField() is_active = models.BooleanField(default=False) Here Student Model is having state and city as positiveInteger which provides the ID of state model and city model. In this case, when the student details required for every object we should query again to get state name and city name. By using extra({}) in the Django ORM we can filter state and city objects with single query students = Student.objects.filter( is_active=True, ).extra( select={ 'state': … -
Getting a MUD Roleplaying Scene going
Getting a MUD RP-scene goingThis article is a little different from the normal more technical Evennia-specific content of this blog. It was originally published as a light-hearted addition to the Imaginary Realities e-zine many years ago. While IR is still online it has since dozed off. So I'm reposting it here to bring it to a new audience. In roleplay-heavy MUDs (and in other categories of text-based roleplaying games), the concept of scenes become important. A scene in this concept is simply a situation big or small that involves you and your fellow players in interesting role play. A scene can be as simple as two players meeting in the street and exchanging a few words to the dramatic conclusion to a staff-driven quest. Whenever role player-interested players meet, a scene may happen.But sometimes scenes won’t come naturally. Sometimes your favourite game has only a few people online, or most of them are hovering in private areas. It’s time to go proactive. Below I offer some archetypes, tropes and ideas for how to get a random Scene started and people interested. The list is based on one I did for a RP-heavy MUD I played some time back.Some terms used … -
django-cache-memoize
Released a new package today: django-cache-memoize Docs On GitHub On PyPI It's actually quite simple; a Python memoize function that uses Django's cache plus the added trick that you can invalidate the cache my doing the same function call with the same parameters if you just add .invalidate to your function. The history of it is from my recent Mozilla work on Symbols. I originally copy and pasted the snippet out that in a blog post and today I extracted it out into its own project with tests, docs, CI and a setup.py. I'm still amazed how long it takes to make a package with all the "fluff" around it. A lot of the bits in here (like setup.py and pytest.ini etc) are copied from other nicely maintained Python packages. For example, I straight up copied the tox.ini from Jannis Leidel's python-dockerflow. The ratio of actual code writing (including tests!) is far overpowered by the package sit-ups. But I "complain with a pinch of salt" because a lot of time spent was writing documentation and that's equally as important as the code probably. -
PyCon.de keynote: Artificial intelligence,: differentiating hype and real value - Michael Feindt
(One of my summaries of a talk at the 2017 PyCon.de conference). He's a physics professor and started out with particle physics. The big experiments like in CERN. Big experiments that also generated Big Data. Think terabyte per second. This was long before the term "Big Data" was invented. Lots of data, you have to filter out the noise and find the actual signal. There's a fine balance there: if you are too careful, you'll never discover anything. If you're too enthousiastic, you can get wrong results. Wrong results are bad for your physics career, so the methods used were quite conservative. He had to fight to get more modern methods like neural networks accepted. What is intelligence? Two definitions: The ability to achieve complex goals. Ability to acquire and apply knowledge and skills. And artificial intelligence? All intelligence that is not biological. Biology, ok, what is life? "A process that retains its pcomplexity and replicates. DNA is abog 1.2GB. This is the physical life. Your brain is about 100TB. This is the "software". Cultural live. It accelerates through teaching, books, technology. Technological life? That will be when it can design its own hardware and software. He guesses robots/computers will … -
PyCon.de: Python on bare metal, micropython on the pyboard - Christine Spindler
(One of my summaries of a talk at the 2017 PyCon.de conference). (See also yesterday's talk) There's a lot of power and functionality in microcontrollers nowadays. But they are harder and harder to program. Wouldn't python be a great fit? It allows beginners to do things they couldn't do before. Micropython is a powerful and modern language with a large community, especially intended for very constrained/embedded systems. If you program for embedded systems, you really have to know the hardware. This is different from "regular" programming. Micropython started with a succesful kickstarter. In 2016, the BBC used it for 7 million school children. There was also a kickstarter for porting it to the super cheap ESP8266 chip. Fun facts: ESA (European space agency) is sponsoring development to make it even more reliable. They're planning to use it in satellites. It is certified for use in traffic management devices in the UK! There were some pyboards and people could play with it. Very nice is that you don't need an IDE: you can just connect to the board and type around on the python prompt. Photo explanation: some 1:87 scale figures on my model railway (under construction). -
PyCon.de: Observing your applications with Sentry and Prometheus - Patrick Mühlbauer
(One of my summaries of a talk at the 2017 PyCon.de conference). Monitoring your applications is important. You can fix problems before they happen. You can quickly pin-point them if they occur anyway. And you should get a good feel for the application through metrics. There are three 'pillars of observability': Logging. Records of individual events that happened. Metrics. Numbers describing a particular process or activity. CPU load, for instance. Tracing. Capture the lifetime of requests as they flow through the various components of a distributed system. (He won't talk about this). Error logging Logging in on a server and searching through logfiles is not much fun. Much better: sentry. It sends notifications for events (mail, slack, etc). It sends them only once. This is important. It aggregates events (statistics, regressions). There are lots of clients for multiple languages (python, javascript, etc.) and platforms (django, flask, angular). It is open source. There is also a software-as-a-service. He showed the user interface for an error. You see the line in your python code where the error occurred. Traceback. Statistics about browser types, server names, and so on. How often the error occurred already. When it occurred. It is easy to integrate. …