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.

Categories
Python Answers

How to populate initial values on Python Django forms?

Sometimes, we want to populate initial values on Python Django forms.

In this article, we’ll look at how to populate initial values on Python Django forms.

How to populate initial values on Python Django forms?

To populate initial values on Python Django forms, we can create a form instance with the initial argument set.

For instance, we write

def view(request):
    game = Game.objects.get(id=1) # just an example
    data = {'id': game.id, 'position': game.position}
    form = UserQueueForm(initial=data)
    return render_to_response('my_template.html', {'form': form})

in the view view function.

In it, we create the UserQueueForm with the initial argument set to the data dict to set the initial value of the form fields with name id and position.

And then we call render_to_response with the template path and a dict with the form in it.

Then in my_template.html, we render it with

{{ form }}

Conclusion

To populate initial values on Python Django forms, we can create a form instance with the initial argument set.

Categories
Python Answers

How to execute raw SQL in Python Flask-SQLAlchemy app?

Sometimes, we want to execute raw SQL in Python Flask-SQLAlchemy app.

In this article, we’ll look at how to execute raw SQL in Python Flask-SQLAlchemy app.

How to execute raw SQL in Python Flask-SQLAlchemy app?

To execute raw SQL in Python Flask-SQLAlchemy app, we can call db.session.execute.

For instance, we write

result = db.session.execute('SELECT * FROM my_table WHERE my_column = :val', {'val': 5})

to call db.session.execute with a SQL string with the :val placeholder.

Then we set the value of val in the dictionary in the 2nd argument.

The selected results are returned.

Conclusion

To execute raw SQL in Python Flask-SQLAlchemy app, we can call db.session.execute.