Django:
delete one table in django sqlite: SO comment
$ 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 & Django official doc - create
signal.py
file in the same directory asmodels.py
andurls.py
etc. - edit the file, write your code
-
add following code in
apps.py
file under the same directory
def ready(self): import appName.signals # noqa
- register in
settings.py
, inINSTALLED_APPS
, register'appName.apps.appNameConfig',
for signal.py to work properly
follow the four steps
- ### models -
DateTimeField(auto_now=True)
andDateTimeField(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
-
context processor
- adding a context processor to your project if you find repetitive bahavior in passing the same context to different views:
- in the same directory of
models.py
create a file namedcontext_processors.py
- in the same directory of
- create a function:
from .models import Category from django.template.context_processors import request def category_context_processor(request): categories = Category.objects.all() return {'categories': categories}
- adding a context processor to your project if you find repetitive bahavior in passing the same context to different views:
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
<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
- To optimize database queries: article by Victor Freitas
- Use
select_related
to avoid unnecessary queries - Use Debug Toolbar to track the queries
- Use
Top comments (0)