Categories
Python Answers

How to extend the User model with custom fields in Python Django?

To extend the User model with custom fields in Python Django, we can extend the AbstractBaseUser class.

For instance, we write

from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)

class MyUser(AbstractBaseUser):
    email = models.EmailField(
                        verbose_name='email address',
                        max_length=255,
                        unique=True,
                    )
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['date_of_birth']

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

to create the MyUser model class which is a child of the AbstractBaseUser model class.

In it, we add new fields and methods which we use as computed properties that combines values from other class fields.

We have the email, date_of_birth, is_active, and is_admin` fields.

And the class methods are computed properties.

Categories
Python Answers

How to combine two or more querysets in a Python Django view?

To combine two or more querysets in a Python Django view, we can use the itertools chain method.

For instance, we write

from itertools import chain
result_list = list(chain(page_list, article_list, post_list))

to call chain with the page_list, article_list, and post_list querysets.

Then we convert the combined querysets into a list with list.

Categories
JavaScript Answers

How to get HTTP method in controller with Express.js?

Sometimes, we want to get HTTP method in controller with Express.js and JavaScript.

In this article, we’ll look at how to get HTTP method in controller with Express.js and JavaScript.

How to get HTTP method in controller with Express.js and JavaScript?

To get HTTP method in controller with Express.js and JavaScript, we can use the req.method property.

For instance, we write

const register = (req, res) => {
  if (req.method == "POST") {
    //...
  }
  res.render('user/registration.html', {
    form: form.toHTML()
  });
}

app.get('/register', register)
app.post('/register', register)

to define the register route handler function.

In it, we check the HTTP method used to make the request with req.method.

Then we can use the same route handler function for routes with different methods.

Conclusion

To get HTTP method in controller with Express.js, we can use the req.method property.

Categories
JavaScript Answers

How to get rid of X-Powered-By response header in Node.js Express and JavaScript?

Sometimes, we want to get rid of X-Powered-By response header in Node.js Express and JavaScript.

In this article, we’ll look at how to get rid of X-Powered-By response header in Node.js Express and JavaScript.

How to get rid of X-Powered-By response header in Node.js Express?

To get rid of X-Powered-By response header in Node.js Express, we can use the app.disable method.

For instance, we write

app.disable('x-powered-by');

to disable the 'x-powered-by' option which removes the X-Powered-By response header in our Express app.

Conclusion

To get rid of X-Powered-By response header in Node.js Express, we can use the app.disable method.

Categories
JavaScript Answers

How to replace a text node with HTML text in JavaScript?

Sometimes, we want to replace a text node with HTML text in JavaScript.

In this article, we’ll look at how to replace a text node with HTML text in JavaScript.

How to replace a text node with HTML text in JavaScript?

To replace a text node with HTML text in JavaScript, we can set the innerHTML property of the element to the HTML we want.

For instance, we write:

<span>
  https://example.com
</span>

to add a span.

Then we replace the HTML inside the span by writing:

const span = document.querySelector('span')
span.innerHTML = `<a href='${span.textContent}'>${span.textContent}</a>`

We get the span with querySelector.

Then we set the innerHTML property of the span to the HTML we want to replace it with.

As a result, we see the link rendered in the span.

Conclusion

To replace a text node with HTML text in JavaScript, we can set the innerHTML property of the element to the HTML we want.