Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Equivalents in Python and JavaScript. Part 3
This is the 3rd part of 4-article series about analogies in Python and JavaScript. In the previous parts we covered a large part of the traditional Python 2.7 and JavaScript based on the ECMAScript 5 standard. This time we will start looking into Python 3.6 and JavaScript based on the ECMAScript 6 standard. ECMAScript 6 standard is pretty new and supported only the newest versions of browsers. For older browsers you will need Babel to compile your next-generation JavaScript code to the cross-browser-compatible equivalents. It opens the door to so many interesting things to explore. We will start from string interpolation, unpacking lists, lambda functions, iterations without indexes, generators, and sets! Variables in strings The old and inefficient way to build strings with values from variables is this concatenation: name = 'World'value = 'Hello, ' + name + '!\nWelcome!' This can get very sparse and difficult to read. Also it is very easy to miss whitespaces in the sentence around variables. Since Python version 3.6 and JavaScript based on the ECMAScript 6 standard, you can use so called string interpolation. These are string templates which are filled in with values from variables. In Python they are also called f-string, because … -
Plain text pypi description formatting: possible cause
Sometimes projects have a plaintext description on pypi.org. You see the restructuredtext formatting, but it isn't applied. See my own z3c.dependencychecker 2.4.2 for example. I use an editor with restructuredtext syntax highlighting. I double-checked everything. I used docutils. I used pypi's own readme_renderer to check it. Not a single error. But still, after some tries, the formatting was still off. Then a fellow contributor adjusted one of the setup() keywords in our setup.py: something might have to be a string (as in another package, with a perfectly-rendering description) instead of a list (as we had). Sadly, no luck. But it made me investigate the other keyword arguments. I noticed, next to long_description, the description. This is normally a one-line description. In our case, it was a multi-line string: long_description = u'\n\n'.join([ ... some files .... ]) description = """ Checks which imports are done and compares them to what's in setup.py and warns when discovering missing or unneeded dependencies. """ setup( name='z3c.dependencychecker', version=version, description=description, long_description=long_description, I changed it to a single line and yes, the next release worked fine on pypi! (Note that I also toyed a bit with encodings in that pull request, but I don't think that had … -
Equivalents in Python and JavaScript. Part 2
Last time we started a new series of articles about analogies in Python and JavaScript. We had a look at lists, arrays, dictionaries, objects, and strings, conditional assignments, and parsing integers. This time we will go through more interesting and more complex things like serializing dictionaries and lists to JSON, operations with regular expressions, as well as raising and catching errors. JSON When working with APIs it is very usual to serialize objects to JSON format and be able to parse JSON strings. In Python it is done with the json module like this: import jsonjson_data = json.dumps(dictionary, indent=4)dictionary = json.loads(json_data) Here we'll indent the nested elements in the JSON string by 4 spaces. In JavaScript there is a JSON object that has methods to create and parse JSON strings: json_data = JSON.stringify(dictionary, null, 4);dictionary = JSON.parse(json_data); Splitting strings by regular expressions Regular expressions are multi-tool that once you master, you can accomplish lots of things. In the last article, we saw how one can join lists of strings into a single string. But how can you split a long string into lists of strings? What if the delimiter can be not a single character as the comma, but a … -
A few JavaScript Functions for Images and Files
> This post will be updated as... -
Equivalents in Python and JavaScript. Part 1
Although Python and JavaScript are quite different languages, there are some analogies which full stack Python developers should know when developing web projects. In this series of 4 parts, I will explore what is similar in each of those languages and what are the common ways to solve common problems. This is not meant to be a reference and I will skip the basics like primitive variable types, conditions, and loops. But I will dig into more complex structures and data operations using both, Python and JavaScript. Also, I will try to focus on the practical use cases. This series should be interesting for the developers of Django, Flask, or another Python framework who want to get a grasp of traditional and modern vanilla JavaScript. On the other hand, it will be useful for the front-enders who want to better understand how the backend is working and maybe even start their own Django website. Parsing integers We'll begin with integer parsing. In Python that's straightforward: number = int(text) But in JavaScript you have to explain what number system you expect: decimal, octal, hexadecimal, or binary: number = parseInt(text, 10); To use the "normal" decimal number system we are passing number … -
python django Automatic Testing part 4
Know the testing outputThe testing output of django here we will see that when we run our tests we will see number of test messages as the test runner prepares itself .Test runner is a component which orchestrates the execution of tests and provides the outcome to the user it may use a graphical interface a textual interface or return a special value to indicate the results of executing the tests If you wish you can control the level of detail of these messages with the verbosity option on the command line like you can run tests with more detail (higher verbosity) by passing in the -v flag some of the other options are-b,--buffer:the standard output and standard error streams are buffered during the test run output during a passing test is discarded output is echoed normally on test fail or error and is added to the failure messages-c,--catch: control c during the test run waits for the current test to end and then reports all the results so far A second control-c raises the normal KeyboardInterrupt exception-f --failfast: stop the test run on the first error or failure--locals: show local variables in tracebacksand you can get all the command … -
A good Django view function cache decorator for http.JsonResponse
I use this a lot. It has served me very well. The code: import hashlib import functools import markus # optional from django.core.cache import cache from django import http from django.utils.encoding import force_bytes, iri_to_uri metrics = markus.get_metrics(__name__) # optional def json_response_cache_page_decorator(seconds): """Cache only when there's a healthy http.JsonResponse response.""" def decorator(func): @functools.wraps(func) def inner(request, *args, **kwargs): cache_key = 'json_response_cache:{}:{}'.format( func.__name__, hashlib.md5(force_bytes(iri_to_uri( request.build_absolute_uri() ))).hexdigest() ) content = cache.get(cache_key) if content is not None: # metrics is optional metrics.incr( 'json_response_cache_hit', tags=['view:{}'.format(func.__name__)] ) return http.HttpResponse( content, content_type='application/json' ) response = func(request, *args, **kwargs) if ( isinstance(response, http.JsonResponse) and response.status_code in (200, 304) ): cache.set(cache_key, response.content, seconds) return response return inner return decorator To use it simply add to Django view functions that might return a http.JsonResponse. For example, something like this: @json_response_cache_page_decorator(60) def search(request): q = request.GET.get('q') if not q: return http.HttpResponseBadRequest('no q') results = search_database(q) return http.JsonResponse({ 'results': results, }) The reasons I use this instead of django.views.decorators.cache.cache_page() is because of a couple of reasons. cache_page generates cache keys that don't contain the view function name. cache_page tries to cache the whole http.HttpResponse instance which can't be serialized if you use the msgpack serializer. cache_page also sends Cache-Control headers which is not … -
Checking if you're pwned (with Django)
Back in March I announced the release of a couple security-related projects for Django, one that implements the Referrer-Policy header, and one that uses the Pwned Passwords database of Have I Been Pwned to check users’ passwords. Today I’ve bumped the version and rolled a new release of pwned-passwords-django; if you’re reading this, version 1.2 is on the Python Package Index, and is only a pip install away. And, of course, there’s full documentation available ... Read full entry -
Make ALL Your Django Forms Better
Website experiences need to be consistent as much as they need to be well thought out and aesthetically pleasing. Structure, visual design, user interactions, and accessibility concerns are among many considerations that go into building quality websites. While achieving consistency of experience and implementation is an essential goal of web development, efficiency of execution is also very important. An efficient workflow means this consistent experience doesn’t require redoing work across the site. This post is about efficient consistency when building forms across your site. Django helps you build forms, but one size doesn’t fit all. It can render your forms on its own, or you can take more control of the form markup in your HTML. When Django renders your forms, you adhere to its defaults and assumptions. When those don’t match your site’s designs or other requirements, you have to do it yourself. But you can squeeze more flexibility out of Django’s own form rendering. This lets you match your form styles and implementations site-wide without losing the valuable tools Django has out-of-the-box to make form rendering easier. Why change what Django does? Maybe you’ve always been fine using the forms exactly as Django renders them for you. Or, … -
Make ALL Your Django Forms Better
Website experiences need to be consistent as much as they need to be well thought out and aesthetically pleasing. Structure, visual design, user interactions, and accessibility concerns are among many considerations that go into building quality websites. While achieving consistency of experience and implementation is an essential goal of web development, efficiency of execution is also very important. An efficient workflow means this consistent experience doesn’t require redoing work across the site. This post is about efficient consistency when building forms across your site. Django helps you build forms, but one size doesn’t fit all. It can render your forms on its own, or you can take more control of the form markup in your HTML. When Django renders your forms, you adhere to its defaults and assumptions. When those don’t match your site’s designs or other requirements, you have to do it yourself. But you can squeeze more flexibility out of Django’s own form rendering. This lets you match your form styles and implementations site-wide without losing the valuable tools Django has out-of-the-box to make form rendering easier. Why change what Django does? Maybe you’ve always been fine using the forms exactly as Django renders them for you. Or, … -
python django Automated testing part 3(please read my previous post ))
Finding data form production database when running tests if the code attempts to access the database when its modules are compiled this will occur before the test database is set up with potentially unexpected results For example if we have a database query in the module level code and a real database exists . then the production data could pollute the tests it is a bad idea to have such import time database queries in your codeso we should have to rewrite the code so that it doesn't do this. This also applies to customized implementation of ready()Order of Test executionDjango test runner reorders tests asAll the TestCase subclasses are run firstThen all other Django-based tests (test case based on SimpleTestCase,including TransactionTestCase) are run with no particular ordering guaranteed nor enforced among themthen any other unittest.TestCase test (including doctests) which may alter the database without restoring it to its orginal state are runNote: The new ordering of tests may reveal unexpected dependencies on test case ordering this is the case with doctests that relied on state left in the database by a given TransactionTestCase test they must be updated to be able to run independentlyTo ensure your tests are … -
Data Filtering in a Django Website using Elasticsearch
In my Web Development with Django Cookbook section Forms and Views there is a recipe Filtering object lists. It shows you how to filter a Django QuerySet dynamically by different filter parameters selected in a form. From practice, the approach is working well, but with lots of data and complex nested filters, the performance might get slow. You know - because of all those INNER JOINS in SQL, the page might take even 12 seconds to load. And that is not preferable behavior. I know that I could denormalize the database or play with indices to optimize SQL. But I found a better way to increase the loading speed. Recently we started using Elasticsearch for one of the projects and its data filtering performance seems to be enormously faster: in our case, it increased from 2 to 16 times depending on which query parameters you choose. What is Elasticsearch? Elasticsearch is java-based search engine which stores data in JSON format and allows you to query it using special JSON-based query language. Using elasticsearch-dsl and django-elasticsearch-dsl, I can bind my Django models to Elasticsearch indexes and rewrite my object list views to use Elasticsearch queries instead of Django ORM. The API … -
python django Automated testing part 2
Running Testwe have learned about tests in the previous lesson now here we can see how to run the test to run the test write this code in the commandline as $/. manage.py testThe test are discovered with using the built in test discovery which by default in python which will discover any file named test.py under the current working directory we can also specify the specific that is required in by supplying any number of test labels to ./manage.py test which are a full python dotted path to a package, module, Test case subclasses or test methods like in here# to run all the tests in the vehicles.tests module$ ./manage.py test vehicles.tests#to run all the tests in the vehicle package$./manage.py test vehicles# to run one test case$./ manage.py test vehicles.tests.VehicleTestCase#to run just one test method$ ./manage.py test vehicles.tests.VehicleTestCase.test_vehicle_can_fly#to provide a path to a directory to discover tests below that directory$ ./manage.py test vehicles/we can also specify a custom file name pattern using the -p (or --pattern) option if your test files are named differently from the test*.py pattern $ ./manage.py test --pattern = *tests_*.py*Test which requires databases ( namely model test) will not use the real production database they … -
Django Tips & Tricks #10 - Log SQL Queries To Console
Django ORM makes easy to interact with database. To understand what is happening behing the scenes or to see SQL performance, we can log all the SQL queries that be being executed. In this article, we will see various ways to achieve this. Using debug-toolbar Django debug toolbar provides panels to show debug information about requests. It has SQL panel which shows all executed SQL queries and time taken for them. When building REST APIs or micro services where django templating engine is not used, this method won't work. In these situations, we have to log SQL queries to console. Using django-extensions Django-extensions provides lot of utilities for productive development. For runserver_plus and shell_plus commands, it accepts and optional --print-sql argument, which prints all the SQL queries that are being executed. ./manage.py runserver_plus --print-sql ./manage.py shell_plus --print-sql Whenever an SQL query gets executed, it prints the query and time taken for it in console. In [42]: User.objects.filter(is_staff=True) Out[42]: SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."is_staff" = true LIMIT 21 Execution time: 0.002107s [Database: default] <QuerySet [<User: anand>, <User: chillar>]> Using django-querycount Django-querycount provides a middleware to show SQL query count and … -
Django Tips & Tricks #10 - Log SQL Queries To Console
Django ORM makes easy to interact with database. To understand what is happening behing the scenes or to see SQL performance, we can log all the SQL queries that be being executed. In this article, we will see various ways to achieve this. Using debug-toolbar Django debug toolbar provides panels to show debug information about requests. It has SQL panel which shows all executed SQL queries and time taken for them. When building REST APIs or micro services where django templating engine is not used, this method won't work. In these situations, we have to log SQL queries to console. Using django-extensions Django-extensions provides lot of utilities for productive development. For runserver_plus and shell_plus commands, it accepts and optional --print-sql argument, which prints all the SQL queries that are being executed. ./manage.py runserver_plus --print-sql ./manage.py shell_plus --print-sql Whenever an SQL query gets executed, it prints the query and time taken for it in console. In [42]: User.objects.filter(is_staff=True) Out[42]: SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."is_staff" = true LIMIT 21 Execution time: 0.002107s [Database: default] <QuerySet [<User: anand>, <User: chillar>]> Using django-querycount Django-querycount provides a middleware to show SQL query count and … -
Vue Tutorial with Django RESTful API — Vue CLI 3 Tutorial In Depth
Throughout this Vue tutorial with Django RESTful API, we are going to learn to use the Vue CLI v3 (In beta — 3.0.0-beta.15 as of this writing) to generate our Vue front-end application. In this tutorial, we're going to see: How to install the latest version of Vue CLI — version 3 How to use various features of the Vue CLI v3 How to use the Vue CLI v3 to create a front-end How to set environment variables for development and production How to add a proxy to forward API calls How to install and add Vue CLI plugins manually etc. The Vue CLI v3 provides a new experience to developers as it allows you to easily generate new Vue projects with zero initial configuration but once your project needs more control you have the possibility to add more configuration options using plugins. Unlike create-react-app, you can customize your configuration without ejecting but only via Vue CLI plugins. The Vue CLI v3 is a complete tooling system that provides many features, out of the box, such as: Interactive project scaffolding via @vue/cli. Zero configuration rapid prototyping via @vue/cli and @vue/cli-service-global. A runtime dependency (@vue/cli-service) that provides many features: It's upgradeable, … -
Full-Stack Vue Tutorial with Django RESTful API — CRUD Using Axios with Vue
Throughout this tutorial with Vue.js and Axios we'll see how to add CRUD (Create, Read, Update and Delete) methods with a Vue front-end and a Python Django RESTful API back-end. Axios is a promise-based modern http client library that runs on the browser and the server through Node.js. Axios works asynchronously and allows you to make HTTP calls to REST endpoints and consume REST APIs. Using Axios with Vue We'll use Axios to send API or HTTP calls (We'll write code for sending GET, POST, DELETE and PUT HTTP requests.) from a Vue.js interface to a RESTful API back-end built with Django. First things first, let's get the back-end code. Getting the Django Back-End In this tutorial, we'll be focusing on building the Vue.js and Axios front-end so we'll be using a previously built Django API available from this GitHub repository Using the following command, you can clone the back-end code: $ git clone https://github.com/techiediaries/django-crm $ cd django-crm Next, you need to create a virtual environment and install packages using pipenv: $ pipenv install Next, activate the virtual environment using: $ pipenv shell Finally, create and migrate the database and run the local development server using: $ python manage.py migrate … -
python django Automated Testing
Automated TestingThe automated testing are simple routines that checks the operation of your code . There are some tests which tests the small part of the code and others are which tests the overall operation of the program automated tests are tests which are done by the system for us like we create a set of tests and then we make changes to our app and test if the code works as if we it were originally intended to work without performing time consuming manual testingBenefits of TestingTest will save Time:If we change any of the components of the code could cause unexpected application behavior checking that it still works means running the test through your code functionality with twenty different variations of your test data just to make sure its working properly this is not a good use of time we can do this test in seconds with automated testing like writing the automated test is more beneficial than testing your application manually Test prevent the problem: they can prevent the coming problems from identifying them early before you could fall in any problemsTest make the code more attractive: if you have made an amazing software then also most developers … -
Handling repetitive tests in Django
When writing tests (unit and/or functional), one of the goal is to cover all edge-cases. DDT is a great way to write such tests; here is how to do so in Python and Django. -
Handling repetitive tests in Django
When writing tests (unit and/or functional), one of the goal is to cover all edge-cases. DDT is a great way to write such tests; here is how to do so in Python and Django. -
Angular 6 Tutorial with Django RESTful API — Adding Angular Material Design 6 UI By Example
In the previous tutorial, you have seen how you can install the Angular CLI 6 and generate a brand new Angular 6 front-end application. In this tutorial, we'll be learning how to use Angular 6 Material Design to build a beautiful and professional grade UI interface. What's Material Design and Angular Material 6 Material Design is a design language created by Google in 2014 that dictates a set of principles and guidelines for creating UIs including motion (animations) and interaction (gestures). Angular Material 6 is the implementation of Material Design for Angular 6. It offers a plethora of components and patterns for navigation, forms, buttons and layouts. In this tutorial, we'll see how to add Material Design to Angular 6 in two ways: The long way: by following a bunch of steps manually. This works for Angular 4, 5 and Angular 6. The short way: by using ng add command to quickly add Angular Material in one step using Angular 6 Schematics. This method only works with Angular 6+. Step1: Installing Angular Material and Angular CDK Head over to your terminal, navigate inside your created Angular front-end application and then run the following commands to install Angular Material 6 and … -
Install Jupyter Notebooks in a Virtual Environment
Let's install Jupyter for a di... -
Angular 6 Tutorial with Django RESTful API — Building Bootstrap 4 UIs
In this Angular 6 tutorial we'll learn how to use Bootstrap 4 with Angular 6 to build professional UIs. Angular 6 is the latest version of Angular when writing this tutorial and Bootstrap 4 is the latest version of Bootstrap — The most popular CSS framework. You can use Bootstrap to create professional looking interfaces without being a CSS designer. In this tutorial, we'll particularly look at how to add Bootstrap 4 to Angular projects generated using Angular CLI 6. In the previous tutorial, we've built a web application with Angular 6 and Django. In this part, we're going to style the UI interface with Bootstrap 4, after installing and setting up the framework in the Angular 6 front-end. In the previous tutorial we’ve: - Installed Angular CLI v6. - Generated a new front-end application using Angular CLI v6. - Created some UI components. In this tutorial, we'll be using the following versions of libraries: Angular 6 and Angular CLI 6. Bootstrap 4. Different Ways to Integrate Bootstrap 4 with Angular 6 There are many ways to add Bootstrap 4 to Angular 6 projects: Installing bootstrap and jquery via npm and add adding scripts and styles to angular.json. Importing bootstrap … -
Django Models Best Practices
A list of tips for designing better models in Python/Django. -
Django CORS
An introduction to CORS and how to enable it in Django.