Django

Django File Upload but request.FILES empty?

  • Posted on: 25 September 2016
  • By: oon
Django File Upload

This is simple but annoying error, when you setting up Django form with File Upload, after you successfully upload the file, but when you check the request.FILES was empty.

What happened?

One of the answer is you probably forget to set encoding type of your form

<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<button type="submit">{% trans 'Submit' %}</button>
</form>

source [1]

Django Edit or Delete Permission

  • Posted on: 18 April 2016
  • By: oon
Django Edit or Delete Permission

If you add Permission in Django via Meta or programatically [1], then you realized that you need to edit or even delete the permission.

How to edit or delete Permissions?

If you are really sure you want to edit or delete it with any risk, you can add Permission to your admin page. [2]

from django.contrib.auth.models import Permission
admin.site.register(Permission)

and of course you should change (or delete it) first on your model or code.

Django REST Framework with ForeignKey Example

  • Posted on: 10 September 2015
  • By: oon

If you are a noob in Django or Django REST Framework (DRF),
it's pretty hard to find (complete) example about providing REST API for system with (sql relationship) foreign key on its database.

this example on SO is quite straigth forward and complete, about DRF with foreignkey.

http://stackoverflow.com/a/19414405/3991504

ya, you also get django.test.Client information as a bonus. nice!

mostly my problem is about reverse relationship [1][2][3].

Django Template Not Support Simple Math Calculation

  • Posted on: 27 May 2015
  • By: oon
Django, web framework for perfectionists with deadlines

I used to use Jinja2 for templating, and it support Math calculation [1].

Now when try Django template, I can't calculate simple math (only add available via builtin filter), so I have to implement filter by myself, or using django-mathfilters [2].

What do you need is only install mathfilters with pip then add mathfilters to INSTALLED_APPS on your settings.

Django Model Serializer

  • Posted on: 5 April 2015
  • By: oon

How to serialize model to JSON on Django?

Django has serializers module on it's core.

from django.core import serializers 
from myproject.myapp import models 
data = serializers.serialize("json", models.MyModel.objects.all())

really simple :)

[1]http://stackoverflow.com/questions/1113096/django-dump-data-for-a-single...

Pages