Friday, September 4, 2020

Django - Duplicate Unformatted Logs

Recently I worked on a bug that was causing duplicate unformatted log messages to appear in a Django app's logs. I made a repository that demonstrates the issue: https://github.com/pawl/django_duplicate_unformatted_logs_example

The problem was caused by an accidental call to logging.info (without using logging.getLogger to get a specific logger) while the root logger isn't already configured.

The solution ended up being to get rid of the accidental calls to logging.info and configuring the root logger to prevent it from accidentally happening again. I go into more details in that repo.

Monday, May 11, 2020

Vue - vue-custom-element

Vue-custom-element seems like the best way to integrate vue into an existing application that uses server side templates.

This approach allows you to use vue components (built with the build pipeline) in your existing html without making your entire application a SPA.

Initially I started following this guide: https://robinverton.de/blog/2018/06/22/django-vue.js-integration-as-a-widget/

Vue-custom-element turned that code into 3 lines and allowed using components like normal (without the data attributes).

This approach seems much better than: https://vsupalov.com/vue-js-in-django-template/ Which doesn't allow for using a build pipeline for babel, linting, testing, single page components, live reload, modules with import/require, etc.

Targeting your builds for web components might do the same thing: https://cli.vuejs.org/guide/build-targets.html#web-component It says it's not compatible with IE11, but that might not be a big deal in most situations.

Tuesday, May 5, 2020

Django - Requiring Prefetching

https://github.com/pawl/django_require_prefetch_select_related_example

I made a repository to try to figure out the best way to require prefech_related/select_related when fetching a model's related objects. I also want to figure out a good pattern for avoiding duplicated prefetch_related/select_related for multiple places in a codebase that need the related objects eager loaded in the same way.


Wednesday, April 29, 2020

Nginx - WebDAV

Just learned Nginx has WebDAV support: https://nginx.org/en/docs/http/ngx_http_dav_module.html

It's a lot faster than the Python package I was using: https://github.com/mar10/wsgidav

Wsgidav is great though. It's super customizable and the documentation is great too.

Wednesday, April 22, 2020

Django - ModelChoiceField queryset caching

It would be nice to know a better way to cache the ModelChoiceField's queryset when it's used in a form that runs "is_valid()" in a loop (like Formsets do). The best way I know how at the moment is by not using a ModelChoiceField at all. The solution requires using a ChoiceField, running the query for choices outside of the loop, then passing the choices into the form to override the ChoiceField choices.

Here's an example:

Django API Forms

Just found a really cool library for using Django's built-in forms to validate JSON: https://github.com/Sibyx/django_api_forms

I made a small example with it to show how it works with ModelChoiceFields: https://github.com/pawl/django_api_forms_modelchoicefield_example

Seems like a more lightweight version of DRF's serializers (but only for deserializing and validating) and with a similar API to Django's built-in forms.

Saturday, April 18, 2020

Learning Vue Through Terrible Pull Requests

I made a pull request asking to "remove the unused public/index.html" file from django-vue-template: https://github.com/gtalarico/django-vue-template/pull/53

Well, the author responded and it turns out it's definitely used (by vue magic): https://cli.vuejs.org/guide/html-and-static-assets.html

Oops, definitely should have googled "vue public/index.html" before making that pull request. I made another pull request to add a link about it to the django-vue-template docs: https://github.com/gtalarico/django-vue-template/pull/54

Saturday, April 11, 2020

Server Side Templates Vs Front End Framework

These Reddit comments have some of the best descriptions I've seen for the type of scenario where you'd use a front end framework over server side templates:

"Server side templating can work really well when the website is static. The problem is that as you add more and more dynamic elements, you either have to go to the server and re-render everything with every change (which can be slow), or you can make the changes on the client side with something like jQuery. But the latter option presents a challenge because you'll eventually find yourself duplicating functionality.

For example, imagine a to-do list website that you render on the server with a templating language. It fetches the user's to-do items from the database and then renders the list. Now let's say you want to let a user add a new item without re-rendering the entire page. You could use AJAX to tell the server to add the new item, but now you need to update your UI list accordingly. You could use jQuery to construct a new DOM element and append it, but now that list element exists in two different places: in your jQuery code and in your template on the server. If your website becomes very dynamic, this situation can get really unwieldy from a programming perspective.

Both Angular (from what I've read) and React (from personal experience) are highly suitable for creating single page apps, with the goal of creating a dynamic, fast, and maintainable website. There are other benefits, as well as downsides, to using Angular and React, but I hope this clears it up a little bit. Both of them make server side templating unnecessary by moving UI logic/rendering from your server to the client's browser. So a user who visits your React powered to-list website would get the React code as a JS bundle, it would make an AJAX call to your server to get just the data, and then it would render the UI based on the data." -/u/VertiGuo (https://www.reddit.com/r/node/comments/6t22cr/back_end_templating_engine_or_front_end_framework/dlhukkf/)

"for example putting together a form, and handle the response is easy, but if you have to manage a list of items where the user can add/remove items, or change the schema of items based on a selector (e.g. phone type or address) you have to split the logic, and put some on client side scripts, which can become very messy. not to mention live updates like chat, comments etc.

creating a separate backend and frontend solves this problem, as the backend's responsibility will be only manipulating the database, retrieve data and run jobs providing an api and the frontend's will be the only the representation of these data, providing the user interface. this separation lets developers use different technologies and languages on the two side, as well as specializing in these. for the beginning this approach is a bit hard to learn, but it will worth it on the long run." -/u/bdvx (https://www.reddit.com/r/webdev/comments/cbrdte/html_templating_vs_spa/etifiar/)

Tuesday, April 7, 2020

Monday, January 13, 2020

Django - ChoiceField Display Text

I made a repo to demonstrate how to get a django ChoiceField's current selection's display text: https://github.com/pawl/django_choicefield_display_example/

I came across a few other examples, but they didn't do a good job of displaying the initial value or handling choices with integer values.