Categories
Python Answers

How to include image files in Django templates?

To include image files in Python Django templates, to set the MEDIA_ROOT and MEDIA_URL settings.

For instance, in settings.py we add

MEDIA_ROOT = '<your_path>/media'
MEDIA_URL = '/media/'

to add MEDIA_ROOT and MEDIA_URL settings to add the media path.

And then we add

urlpatterns = patterns('',
               (r'^media/(?P<path>.*)$', 'django.views.static.serve',
                 {'document_root': settings.MEDIA_ROOT}),
              )

to add the static URL path to serve the image from.

And then in our template, we add

<img src="{{ MEDIA_URL }}<sub-dir-under-media-if-any>/<image-name.ext>" />

to get the image from /media/ with the path to the image.

Categories
Python Answers

How to serialize a model instance in Python Django?

Sometimes, we want to serialize a model instance in Python Django.

In this article, we’ll look at how to serialize a model instance in Python Django.

How to serialize a model instance in Python Django?

To serialize a model instance in Python Django, we can use the serialize method.

For instance, we write

from django.core import serializers

serialized_obj = serializers.serialize('json', [ obj, ])

to call serializers.serialize with 'json' and the obj object that we want to serialize in a list to serialize obj into a JSON string

Conclusion

To serialize a model instance in Python Django, we can use the serialize method.

Categories
Python Answers

How to view corresponding SQL query of the Python Django ORM’s queryset?

To view corresponding SQL query of the Python Django ORM’s queryset, we can use the query property of the query set.

For instance, we write

qs = Model.objects.filter(name='test')
print(qs.query)

to print the SQL query corresponding to the Model.objects.filter(name='test') queryset with

print(qs.query)
Categories
Python Answers

How to fix MySQL “incorrect string value” error when save unicode string in Python Django?

Sometimes, we want to fix MySQL "incorrect string value" error when save unicode string in Python Django.

In this article, we’ll look at how to fix MySQL "incorrect string value" error when save unicode string in Python Django.

How to fix MySQL "incorrect string value" error when save unicode string in Python Django?

To fix MySQL "incorrect string value" error when save unicode string in Python Django, we can change some options in the Django app settings.

To fix this, we write

DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.mysql',
        ...
        'OPTIONS': {'charset': 'utf8mb4'},
    }
}

in settings.py.

We can use utf8mb4 characters with MySQL 5.5 or later.

Conclusion

To fix MySQL "incorrect string value" error when save unicode string in Python Django, we can change some options in the Django app settings.

Categories
Python Answers

How to store a phone number in Python Django models?

To store a phone number in Python Django models, we can use the phonenumber_field libraru.

To install it, we run

pip install django-phonenumber-field[phonenumbers]

Then in settings.py, we add

INSTALLED_APPS = [
    ...
    'phonenumber_field',
    ...
]

to add 'phonenumber_field' into INSTALLED_APPS.

And then in our model, we add

from phonenumber_field.modelfields import PhoneNumberField

class Client(models.Model, Importable):
    phone = PhoneNumberField(null=False, blank=False, unique=True)

to add the phone PhoneNumberField to add a phone number field.

Then we create a form with

from phonenumber_field.formfields import PhoneNumberField
class ClientForm(forms.Form):
    phone = PhoneNumberField()