Find object in list that has attribute equal to some value (that meets any condition)

This gets the first item from the list that matches the condition, and returns None if no item matches. It’s my preferred single-expression form. However, The naive loop-break version, is perfectly Pythonic — it’s concise, clear, and efficient. To make it match the behavior of the one-liner: This will assign None to x if you … Read more

Check if an object exists

Since filter returns a QuerySet, you can use count to check how many results were returned. This is assuming you don’t actually need the results. After looking at the documentation though, it’s better to just call len on your filter if you are planning on using the results later, as you’ll only be making one sql query: A count() call … Read more

How to revert the last migration?

You can revert by migrating to the previous migration. For example, if your last two migrations are: 0010_previous_migration 0011_migration_to_revert Then you would do: You don’t actually need to use the full migration name, the number is enough, i.e. You can then delete migration 0011_migration_to_revert. If you’re using Django 1.8+, you can show the names of all the … Read more

No module named django but it is installed

Probably, pip installs packages into dist-packages directory, which is not included into PYTHONPATH environment variable. You have a couple of solutions: Create and configure virtualenv for your project, before using pip. This is the most Pythonic way Try to install Django using built-in pip module:python -m pip install django This command should install packages into site-packages directory. You may also add dist-packages to your PYTHONPATH. This question should help you: How to globally … Read more

TemplateDoesNotExist at /

Hej! Many threads here had the same caption, but none of them solved my problem. I have a Django site an can access /admin (but it looks ugly). But on / there appears the following error page (DEBUG = True in settings.py): In fact, the file /home/django/django/templates/iecl_dev/index.html does exist and I also tried chmod o+r … Read more