Posts

Showing posts from January, 2023

Python and Django Full Stack - Include Image and CSS References

Image
In the previous blog post , we looked at template tags to render custom text. In this post, we will see how to refer to images and css files.    The steps are the same as for template tags, except for the following differences: 1.       Create a folder called static in the top level project folder. Inside the static directory, create two folders -            i. images to store the pictures. (save any picture e.g. mypicture.jpg in this directory)          ii. css to store the css file (e.g. mystyles.css) 2.       Include  this folder to the settings.py file, so Django knows where to find it. Note that the variable STATIC_URL comes pre-defined from versions 1.9 and up. Ensure there are no duplicate declarations of the variables. STATIC_URL = ' static/ ' STATICFILES_DIRS = [      ' static ', ] 3.       Test if this works, by going ...

Python and Django Full Stack - Templates

Image
  Templates in Django Templates can be understood as a skeleton of the page. There are two parts to a templates file – One, the static information, and two, template tags, that inject dynamic data with special syntax, through its views, forming the final HTML. They are key to understanding how Django interacts with the website. Later, we will connect templates with models, so data can be created and displayed dynamically. 1.      Create a new folder – in the base folder first_project/templates/first_app 2.      Let Django know of the templates. Edit the DIR key inside the settings.py to include ‘templates’. TEMPLATES = [     {         ' BACKEND ': ' django.template.backends.django.DjangoTemplates ',         'DIRS': ['templates',],         ' APP_DIRS ': True ,         ' OPTIONS ': {             ' co...