DEV Community

Cover image for Retail Website built with Django P5 (2020-02-16)
Peiwen Li
Peiwen Li

Posted on

Retail Website built with Django P5 (2020-02-16)

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
Enter fullscreen mode Exit fullscreen mode

  • ### where should signal.py file lives: article by Vitor Freitas & Django official doc
  • create signal.py file in the same directory as models.py and urls.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
    
  1. 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


  • 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
    • 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}
    
3. add this processor to `settings.py`
Enter fullscreen mode Exit fullscreen mode
```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',
                ],
            },
        },
    ]
```
Enter fullscreen mode Exit fullscreen mode

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">
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)