Daily Notes: Retail Website in Russian P6
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
Django
-
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 directorydef ready(self): import appName.signals # noqa
- register in
settings.py
, inINSTALLED_APPS
, register'appName.apps.appNameConfig',
for signal.py to work properly
- create
follow the four steps
-
models -
DateTimeField(auto_now=True)
andDateTimeField(auto_now_add=True)
DateTimeField(auto_now=True)
– update everytime this model instance is editedDateTimeField(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
2. create a function: ```python from .models import Category from django.template.context_processors import request
- in the same directory of
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. - adding a context processor to your project if you find repetitive bahavior in passing the same context to different views: