<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Django on KK's Blog (fromkk)</title><link>https://fromkk.com/tags/django/</link><description>Recent content in Django on KK's Blog (fromkk)</description><generator>Hugo</generator><language>en</language><managingEditor>bebound@gmail.com (KK)</managingEditor><webMaster>bebound@gmail.com (KK)</webMaster><lastBuildDate>Sun, 10 Aug 2025 18:44:05 +0800</lastBuildDate><atom:link href="https://fromkk.com/tags/django/index.xml" rel="self" type="application/rss+xml"/><item><title>How to disable auto strip in Charfield in Django</title><link>https://fromkk.com/posts/how-to-disable-auto-strip-in-charfield-in-django/</link><pubDate>Sun, 19 Dec 2021 21:20:00 +0800</pubDate><author>bebound@gmail.com (KK)</author><guid>https://fromkk.com/posts/how-to-disable-auto-strip-in-charfield-in-django/</guid><description>&lt;p&gt;In Django, when edit field in admin page or post data to forms, the leading and tailing whitespace in &lt;code&gt;CharField&lt;/code&gt; and &lt;code&gt;TextField&lt;/code&gt; are removed.&lt;/p&gt;
&lt;p&gt;The reason is &lt;code&gt;strip=True&lt;/code&gt; parameter in &lt;code&gt;forms.CharField&lt;/code&gt;, which is added in Djagno 1.9. You can see the discussion in &lt;a href="https://code.djangoproject.com/ticket/4960" target="_blank" rel="noopener noreffer "&gt;django tiket #4960&lt;/a&gt; and here is &lt;a href="https://github.com/django/django/blob/4ce59f602ed28320caf3035212cb4d1c5430da2b/django/forms/fields.py#L211" target="_blank" rel="noopener noreffer "&gt;source code&lt;/a&gt;. &lt;code&gt;models.CharField&lt;/code&gt; and &lt;code&gt;models.TextField&lt;/code&gt; use &lt;code&gt;formfield()&lt;/code&gt; to create form to interact with user, then both of them eventually create a &lt;code&gt;forms.CharField&lt;/code&gt;&lt;/p&gt;</description></item><item><title>Using JSONField before Django 3.1</title><link>https://fromkk.com/posts/using-jsonfield-before-django-3-dot-1/</link><pubDate>Sat, 11 Sep 2021 21:12:00 +0800</pubDate><author>bebound@gmail.com (KK)</author><guid>https://fromkk.com/posts/using-jsonfield-before-django-3-dot-1/</guid><description>&lt;p&gt;In Django 3.1, Django support save python data into database as JSON encoded data and it is also possible to make query based on field value in JSONField. The detailed usage can be found &lt;a href="https://docs.djangoproject.com/en/3.2/topics/db/queries/#querying-jsonfield" target="_blank" rel="noopener noreffer "&gt;here&lt;/a&gt;. If you are using older version and want to try this feature. Though there are many packages ported this function, I recommend &lt;a href="https://github.com/laymonage/django-jsonfield-backport" target="_blank" rel="noopener noreffer "&gt;django-jsonfield-backport&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="django-jsonfield-backport"&gt;django-jsonfield-backport&lt;/h2&gt;
&lt;p&gt;This package save data as JSON in database and also support JSON query. If your database meet the requirements (MySQL &amp;gt; 5.7, PG &amp;gt; 9.5, MariaDB &amp;gt; 10.2 or SQLite &amp;gt; 3.9 with &lt;a href="https://docs.djangoproject.com/en/3.1/ref/databases/#sqlite-json1" target="_blank" rel="noopener noreffer "&gt;JSON1&lt;/a&gt; extension), you can use JSONField like Django&amp;rsquo;s native implementation.&lt;/p&gt;</description></item><item><title>CSRF in Django</title><link>https://fromkk.com/posts/csrf-in-django/</link><pubDate>Wed, 07 Nov 2018 13:58:00 +0800</pubDate><author>bebound@gmail.com (KK)</author><guid>https://fromkk.com/posts/csrf-in-django/</guid><description>&lt;p&gt;CSRF(Cross-site request forgery) is a way to generate fake user request to target website. For example, on a malicious website A, there is a button, click it will send request to &lt;a href="https://www.B.com/logout" target="_blank" rel="noopener noreffer "&gt;www.B.com/logout&lt;/a&gt;. When the user click this button, he will logout from website B unconsciously. Logout is not a big problem, but malicious website can generate more dangerous request like money transfer.&lt;/p&gt;
&lt;h2 id="django-csrf-protection"&gt;Django CSRF protection&lt;/h2&gt;
&lt;p&gt;Each web framework has different approach to do CSRF protection. In Django, the validation process is below:&lt;/p&gt;</description></item></channel></rss>