# Topic covered
* Authentication
* Authentication vs Authorization
* How authentication is determined
* Setting the authentication scheme
* Types of Authentication
* TokenAuthentication
* SessionAuthentication
* BasicAuthentication
* Permissions
[Read More]
9. TokenAuthentication
Django-rest-auth, Dj-rest-auth
# Topic covered
* TokenAuthentication
* Django-rest-auth
[Read More]
01-Django Adv
Django postgres full text search
Django postgres full text search
- Unlike relational databases, full-text search is not standardized.
- There are several open-source options like ElasticSearch, Solr, and Xapian.
- For general uses
- We can stick with the full-text search capabilities that many relational (like Postgres, MySQL, SQLite)
- And non-relational databases (like MongoDB and CouchDB) offer.
Basic Search
- In Django you’ll typically start by performing search queries with
containsoricontainsfor exact matches. - The
Qobject can be used as well to addAND(&) orOR(|) logical operators. - Limitations
- Only exact match is shown –> similar words excluded
- Stop word(article) all has same weight
- complex lookups not possible
from django.db.models import Q
ProductDetail.objects.filter(Q(name__icontains=query))
Quote.objects.filter(Q(name__icontains=query) | Q(quote__icontains=query))
- Eg:
- query = pony
- then ponies != pony
The search lookup(Single Field Search)
- To use the search lookup,
django.contrib.postgresmust be in your INSTALLED_APPS. - Used for Single Field Search
- Advantage
- It takes similar words into account
- ponies, pony
- It takes similar words into account
INSTALLED_APPS += ['django.contrib.postgres']
ProductDetail.objects.filter(name__search=query)
SearchVector
- To search against multiple fields and on related models, you can use the SearchVector
Entry.objects.annotate(search=SearchVector('body_text') + SearchVector('blog__tagline')).filter(search=query)
Quote.objects.annotate(search=SearchVector("name", "quote")).filter(search=query)
SearchQuery
SearchQuery translates the terms the user provides into a search query object that the database compares to a search vector. By default, all the words the user provides are passed through the stemming algorithms, and then it looks for matches for all of the resulting terms.
[Read More]03-Django Adv
𝗖𝗿𝗼𝘀𝘀-𝗢𝗿𝗶𝗴𝗶𝗻 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲 𝗦𝗵𝗮𝗿𝗶𝗻𝗴 (𝗖𝗢𝗥𝗦)
𝗖𝗿𝗼𝘀𝘀-𝗢𝗿𝗶𝗴𝗶𝗻 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲 𝗦𝗵𝗮𝗿𝗶𝗻𝗴 (𝗖𝗢𝗥𝗦)
CORS is a security mechanism enforced by web browsers to restrict cross-origin HTTP requests.
CORS is a mechanism implemented by browsers that block websites to request data from some other URL.
When a browser makes a request, it adds an origin header to the request message.
If it goes to the server of the exact origin, it is allowed by the browser; if it does not, the browser blocks it.
gets error like Cross-Origin Request Blocked
03-Django Adv
Logging Works in Django
Logger
Print statement generally does not work in production so, to get message we use logger
- Troubleshooting made easier
- Runtime information in production
- https://www.youtube.com/watch?v=ziegOuE7M4A&list=PL2NFhrDSOxgXXUMIGOs8lNe2B-f4pXOX-&index=4
What is Logging?
How Logging Works in Django
- Django comes with
Python's built-in logging moduleto leverage system logging.- Loggers
- Handlers
- Filters
- Formatters
Loggers
- Loggers are basically the entry point of the logging system.
- If the log-level is same or exceeds the log-level of the logger, the message is sent to the handler for further processing.
DEBUG: 10 --> Low-level system information
INFO: 20 --> General system information
WARNING: 30 --> Minor problems related information
ERROR/EXCEPTION: 40 --> Major problems related information
CRITICAL: 50 --> Critical problems related information
dblogger.debug("Test Debug")
dblogger.info("Test Info")
dblogger.warning("Test Warning")
dblogger.warn("Test Warn") # Depricated
dblogger.error("Test Error")
dblogger.exception("Test Exception")
dblogger.critical("Test Critical")
Handlers
Handlers basically determine what happens to each message in a logger eg: write to a file or console.
[Read More]04-Django Adv
Throttling - API rate limiting
Throttling
API rate limiting is a technique used to control the number of requests a user or client can make to an API within a specific time frame. In Django, you can implement rate limiting using third-party libraries like Django REST Framework’s (DRF) throttling or by integrating specialized libraries like django-ratelimit.
4.1 Using Django REST Framework (DRF) Throttling
DRF provides built-in throttling classes to enforce rate limits.
Period should be one of: (’s’, ‘sec’, ’m’, ‘min’, ‘h’, ‘hour’, ’d’, ‘day’)
[Read More]