Retail Website built with Django P5 (2020-02-16)
Tags: django, dailynotes
Published: 2022-05-15 02:41:16


## Django:
---
### delete one table in django sqlite: [SO comment](https://stackoverflow.com/a/17041402/11901269)
```bash
$ python manage.py dbshell
# if you do not know which table to delete/drop, run:
sqlite > SELECT * FROM sqlite_master WHERE type='table';
# if you already know:
sqlite > DROP TABLE appname_modelname;

# DO NOT FORGET the semi-colon at the end, otherwise it will prompt '...>' signalling the expression is unfinished
```
---
* ### where should `signal.py` file lives: 
[article by Vitor Freitas](https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html) & [Django official doc](https://docs.djangoproject.com/en/3.0/topics/signals/)
1. create `signal.py` file in the same directory as `models.py` and `urls.py` etc.
2. edit the file, write your code
3. add following code in `apps.py` file under the same directory
	```python
	def ready(self):
		import appName.signals # noqa
	```
4. register in `settings.py`, in `INSTALLED_APPS`, register `'appName.apps.appNameConfig',` for signal.py to work properly

follow the four steps

---
* ### models - `DateTimeField(auto_now=True)` and `DateTimeField(auto_now_add=True)`
  * `DateTimeField(auto_now=True)` -- update everytime this model instance is edited
  * `DateTimeField(auto_now_add=True)` -- set the time once when this model instance is created

---
* ### SO ask and answered [Django - logout view customisation fail](https://stackoverflow.com/questions/60256470/django-logout-view-customisation-fail/60256968?noredirect=1#comment106584955_60256968)

---
* ### context processor
	* adding a context processor to your project if you find repetitive bahavior in passing the same context to different views:
		1. in the same directory of `models.py` create a file named `context_processors.py`
    2. create a function: 
    ```python
    from .models import Category
		from django.template.context_processors import request

    def category_context_processor(request):
        categories = Category.objects.all()
        return {'categories': categories}
    ```
    3. add this processor to `settings.py`
    ```python
    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                ... # other processors
                ... # add code below:<app-name>.<file-name>.<function-name>
                'app-name.context_processors.category_context_processor',
            		],
        		},
    		},
		]
    ```
Now you don't have to pass `categories` to each view now! And this code will run on every request on the site.


## Bootstrap
---
### to override default
simply add an id to body tag
```html
<body id="bootstrap-override">
```
and refer to this id when overriding default css

---

## General knowledge
---
### url in css:
* Because css is a static file, so the url inside is better be static as well. so just use relative url for url() in css files.
* for some reason it's slow to load a file when you pupdate it in css. sometime I have to restart the local server for it to take effect


### ## [Debug Toolbar](https://django-debug-toolbar.readthedocs.io/en/latest/index.html)

* To optimize database queries: [article](https://simpleisbetterthancomplex.com/tips/2016/05/16/django-tip-3-optimize-database-queries.html) by Victor Freitas
  * Use `select_related` to avoid unnecessary queries
  * Use Debug Toolbar to track the queries
*