Posts

Internationalization (Language Setting) in Django

If your project want to run in English and a Spanish version edit the settings.py file of your project and add the following LANGUAGES setting to it.  Place it next to the LANGUAGE_CODE setting:                LANGUAGES = (                ('en', 'English'),                ('es', 'Spanish'),              ) The LANGUAGES setting contains two tuples that consist of a language code and a name .  Language codes can be locale-specific , such as en-us or en-gb , or generic , such as en . With this setting, we specified that application will only be available in English and Spanish.  If you don't define a custom LANGUAGES setting, the site will be available in all the languages that Django is translated into. Make your LANGUAGE_CODE setting look as follows:            ...

Django Authentication Framework

Django comes with a built-in authentication framework that can handle user authentication, sessions, permissions, and user groups. The authentication system includes views for common user actions such as log in, log out, password change, and password reset. The authentication framework is located at django.contrib.auth and is used by other Django contrib packages.  When you create a new Django project using the startproject command, the authentication framework is included in the default settings of your project. It consists of the django.contrib.auth application and the following 2 middleware classes found in the MIDDLEWARE setting of your project. AuthenticationMiddleware:  This class associates users with requests using sessions SessionMiddleware:  This class handles the current session across requests Middleware are the classes with methods that are globally executed during the request or response phase.  The authentication framework also includes the follo...

reverse_lazy() Method in Django

Use reverse_lazy() to generate the URL for the link attribute. The reverse() method allows you to build URLs by their name and pass optional parameters. The reverse_lazy() utility function is a lazily evaluated version of reverse(). It allows you to use a URL reversal before the project's URL configuration is loaded.

Messages Framework in Django

The Messages Framework is located at django.contrib.messages and it is included in the default INSTALLED_APPS list of settings.py file when we create new projects using python manage.py startproject.  The messages framework provides a simple way to add messages to users. settings file contains a middleware named django.contrib.messages.middleware. MessageMiddleware in the MIDDLEWARE settings. Messages are stored in a cookie by default, and they are displayed in the next request from the user.  The messages framework in your views by importing the messages module and adding new messages with simple shortcuts, as follows:                from django.contrib import messages                messages.error(request, 'Something Went Wrong Here....!') Create new messages using the add_message() method or any of the following shortcut methods: success(): Success messages to be displayed after an a...

NullBooleanField in Django

Django 2.1 introduced  null=True  for  BooleanField . Using  NullBooleanField  is now obsolete.  So use, boolean_field = BooleanField(null=True) instead of  boolean_field  = NullBooleanField()

Advantages to using URLField() over TextField()

URLField is actually CharField which supports Regex-based URL pattern checking and a online validator which was replaced by a RegEx based validator, where as use TextField if you are not consider length-limitation of URL. Use of CharField or TextField depends on whether you want max-length constraint on the field, and which element type is more suitable for editing like textarea or input.          Ex:           from django.core.validators import URLValidator          url_ field = models.TextField(validators=[URLValidator()])

URLField() in Django

 A URLField is a field of a database table or a form which stores only URLs. This field is generally used for storing webpage links called as URLs.  It is validated by URLValidator .  To store larger text, TextField is used. The default form widget for this field is TextInput .           Syntax:                field_name = models.URLField(max_length=250, **options)