Posts

Python Keywords

Below are the Python Key words, which form the basis of the Python language.  You are not allowed to use these words to name your variables, because these are the core commands of Python.  They must be spelled exactly as written here for the interpreter to recognize them as keywords.  The words True, False and None have a special meaning, which will be covered later. False True None and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield

Sending Emails with Django

Image
Sending Emails with Django is pretty straightforward. 1st, you need to have a local SMTP(Simple Mail Transfer Protocol) server, or you need to define the configuration of an external SMTP server by adding the following settings to the settings.py file of your project: EMAIL_HOST: The SMTP server host; the default is localhost . EMAIL_PORT: The SMTP port; the default is 25 . EMAIL_HOST_USER: The username for the SMTP server. EMAIL_HOST_PASSWORD: The password for the SMTP server. EMAIL_USE_TLS: Whether to use a TLS(Transport Layer Security) secure connection. EMAIL_USE_SSL: Whether to use an implicit TLS secure connection. If we can not use an SMTP server, we can tell Django to write emails to the console by adding the following setting to the settings.py file EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' By using this setting, Django will output all emails to the shell. This is very useful for testing your application without an SMTP server. If we want t...

Creating Model Managers in Django

Model Managers: object is the default manager of every model that retrieves all objects in the database.  We can also define custom managers for your models.  We will create a custom manager to retrieve all posts with the published status. There are 2 ways to add or customize managers for your models: you can add extra manager methods to an existing manager, or create a new manager by modifying the initial QuerySet that the manager returns.  The first method provides you with a QuerySet API such as Posts.objects.my_manager() The 2nd method provides you with Posts.my_manager.all().  The manager will allow you to retrieve posts using Posts.published.all(). Edit the models.py file of your <<xx>> application to add the custom manager: class xxManager(models.Manager):      def get_queryset(self):           return super(xxManager,                      ...

QuerySets Evaluation in Django

Creating a QuerySet does not involve any Database(DB) activity until it is evaluated.  QuerySets usually return another unevaluated QuerySet .  You can concatenate as many filters as you like to a QuerySet, and you will not hit the database until the QuerySet is evaluated.  When a QuerySet is evaluated, it translates into an SQL query to the database. QuerySets are only evaluated in the following scenarios: The first time you iterate over them When you pickle or cache them When you  slice  them, for Ex:, Posts.objects.all()[:5] When you call repr() or len() on them When you explicitly call list() on them When you test them in a statement, such as bool() , or, and , or if

delete() method in Django

If you want to delete an object, you can do it from the object instance using the delete() method. Ex:. post = Posts.objects.get(id=1) post.delete()

order_by() method in Django

Order results by different fields using the order_by() method of the manager. For Ex:, you can retrieve all objects ordered by their post_name, as follows: Posts.objects.order_by('post_name') Ascending order is implied. You can indicate descending order with a negative sign prefix. Posts.objects.order_by('-post_name')

exclude() method in Django

You can exclude certain results from the QuerySet using the exclude() method of the manager. For Ex:, you can retrieve all posts published in 2019 whose titles don't start with xyz: Posts.objects.filter(publish__year=2019).exclude(title__startswith='xyz')