Categories
Python Answers

How to override widget in a Python Django ModelForm?

Sometimes, we want to override widget in a Python Django ModelForm.

In this article, we’ll look at how to override widget in a Python Django ModelForm.

How to override widget in a Python Django ModelForm?

To override widget in a Python Django ModelForm, we can set the widgets field to a dict with the field overrides.

For instance, we write

from django.forms import ModelForm, Textarea
from myapp.models import Author

class AuthorForm(ModelForm):
    class Meta:
        model = Author
        fields = ('name', 'title', 'birth_date')
        widgets = {
            'name': Textarea(attrs={'cols': 80, 'rows': 20}),
        }

to create the AuthorForm model form class with a few fields.

We set the widgets field to a dict with the name field overridden with a Textarea with 80 columns and 20 rows.

Conclusion

To override widget in a Python Django ModelForm, we can set the widgets field to a dict with the field overrides.

Categories
Python Answers

How to split string to array with Python Django templates?

Sometimes, we want to split string to array with Python Django templates.

In this article, we’ll look at how to split string to array with Python Django templates.

How to split string to array with Python Django templates?

To split string to array with Python Django templates, we can create our own model method to do the splitting.

For instance, we write

class Game(models.Model):
    #...
    def screenshots_as_list(self):
        return self.screenshots.split('\n')

to create the Game model with the screenshots_as_list method to return the screenshots string field split into an array by the newline.

And then in our template, we use it by writing

{% for screen in game.screenshots_as_list %}
    {{ screen }}<br>
{% endfor %}

Conclusion

To split string to array with Python Django templates, we can create our own model method to do the splitting.

Categories
Python Answers

How to annotate count with a distinct field with Python Django?

Sometimes, we want to annotate count with a distinct field with Python Django.

In this article, we’ll look at how to annotate count with a distinct field with Python Django.

How to annotate count with a distinct field with Python Django?

To annotate count with a distinct field with Python Django, we can call annotate with the distinct argument set to True in the aggregation function.

For instance, we write

p = Project.objects.all().annotate(Count('informationunit__username', 
                                         distinct=True))

to call annotate with the aggregation result returned by Count.

We set distinct to True to annotate count with a distinct field.

Conclusion

To annotate count with a distinct field with Python Django, we can call annotate with the distinct argument set to True in the aggregation function.

Categories
Python Answers

How to fix Python Django cannot find static files?

Sometimes, we want to fix Python Django cannot find static files.

In this article, we’ll look at how to fix Python Django cannot find static files.

How to fix Python Django cannot find static files?

To fix Python Django cannot find static files, we can specify the template directory explicitly.

For instance, in settings.py, we write

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

to set STATICFILES_DIRS to a list of folders with the static file paths.

Categories
Python Answers

How to send mail using Gmail SMTP with Python Django?

Sometimes, we want to send mail using Gmail SMTP with Python Django.

In this article, we’ll look at how to send mail using Gmail SMTP with Python Django.

How to send mail using Gmail SMTP with Python Django?

To send mail using Gmail SMTP with Python Django, we can add the email settings in the settings.

Then we use the EmailMessage class to send the email.

For instance, we write

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'test'
EMAIL_PORT = 587

to add the email settings in settings.py.

Then in our view, we write

from django.core.mail import EmailMessage

email = EmailMessage('title', 'body', to=[email])
email.send()

to create a new EmailMessage instance with the title, body, and the to email addresses.

Then we call send to send the email.

Conclusion

To send mail using Gmail SMTP with Python Django, we can add the email settings in the settings.

Then we use the EmailMessage class to send the email.