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

What does on_delete do on Django models?

This is the behaviour to adopt when the referenced object is deleted. It is not specific to Django; this is an SQL standard. Although Django has its own implementation on top of SQL. (1) There are seven possible actions to take when such event occurs: CASCADE: When the referenced object is deleted, also delete the objects that have … 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

Cannot find command ‘git’ – windows

Download and Install git in your windows from here: Then add its installation bin path to your windows’s environment path. Then you will find the git command at the command prompt globally. This may solve you problem. How to change environment variables : Git: Installing Git in PATH with GitHub client for Windows For Visual … Read more

What is a “slug” in Django?

A “slug” is a way of generating a valid URL, generally using data already obtained. For instance, a slug uses the title of an article to generate a URL. I advise to generate the slug by means of a function, given the title (or another piece of data), rather than setting it manually. An example: … Read more

What is the use of PYTHONUNBUFFERED in docker file?

Setting PYTHONUNBUFFERED to a non empty value ensures that the python output is sent straight to terminal (e.g. your container log) without being first buffered and that you can see the output of your application (e.g. django logs) in real time. This also ensures that no partial output is held in a buffer somewhere and never written … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)