Tag: django

  • Python dict.get() vs dict[‘brackets’], which approach is better?

    Python dict.get() vs dict[‘brackets’], which approach is better?

    by

    in

    Using the .get() method on a dictionary is generally considered a better approach than using the direct dictionary indexing dictionary[‘key’] method because the .get() method provides a default value if the key is not found in the dictionary, whereas direct dictionary indexing raises a KeyError exception. Here is an example: my_dict = {‘a’: 1, ‘b’:…

  • Why its better to use .exists() method on query rather then do try-except block?

    Why its better to use .exists() method on query rather then do try-except block?

    by

    in

    Both of the provided code samples achieve the same result, but they use different approaches to handle the case when the instance with the given primary key does not exist. class Balance(models.Model): current_balance = models.DecimalField(_(“Current Balance”), max_digits=12, decimal_places=2, default=0.00) last_balance = models.DecimalField(_(“Last Balance”), max_digits=12, decimal_places=2, null=True) def save(self, *args, **kwargs): if self.pk: try: prev_state =…

  • Where should I store database queries in Django?

    Where should I store database queries in Django?

    by

    in

    The Django business logic dilemma? In general, it’s a good practice to keep your code organized and modular. If your view code includes complex database queries or large amounts of database-related logic, it might be a good idea to move those queries to a separate file, such as a Django model or manager. Here are…

  • How to check if django-tables2 table is empty or not in templates code?

    How to check if django-tables2 table is empty or not in templates code?

    by

    in

    In Django-Tables2, you can check if a table is empty or not by checking the length of the table’s queryset. If the length is zero, then the table is empty. You can do this in your template code using the if statement and the table.queryset attribute. Here’s an example of how to do this in…

  • Django Rest Framework Tips and Tricks

    Django Rest Framework Tips and Tricks

    by

    in

    In this Django rest framework tips article, I will collect interesting and useful tips that can be used while you code your API with Django. I do also have a Django framework tips and tricks article, don’t forget to check. How to restrict access to Django Rest Framework API for specific users only? I will…

  • Django tips and tricks

    Django tips and tricks

    When to use gettext() and gettext_lazy() Functions for TranslationAll of these snippets, Django tips and tricks have been tested on real-world Django applications by me. I’m using each of them in my SaaS projects. I wrote this article in a specific manner, less text – more code – more solutions! If you have any issue/error…

  • Properly Run Python Django under a local network

    Properly Run Python Django under a local network

    by

    in

    First, you need to find your local IP address with this command in cmd: ipconfig/all Open settings.py and add your IP address to the ALLOWED_HOSTS list: ALLOWED_HOSTS = [‘192.168.xxx.xxx’] Finally run command in the terminal: python manage.py runserver 192.168.xxx.xxx:8000 Voila, now you can visit your Django app by accessing this address(192.168.xxx.xxx:8000) from any device in…