In this mini project, we are going through the basics of django,a full stack framework of Python that can help us create from websites , to simple and complicated applications.Here we use also openweather api for the implementation
To make this little project become reality,you need to:
- Download Django.
- Sign up in Openweather Api,in order to get a key via email.
- main
This is the name of the app and also where the most of things that used for controlling the app take place
- static/main
This is the folder where every css file will be storaged.In the meanwhile it is better to keep the in one file for a better organisation.Also is a static file,meaning that we must every file in a specific way with the directory of the html file,not just the traditional way
- templates
This folder keeps the html file that will be rendered in views.py
- settings.py 🛠️
- First in installed apps add '
nameofyourapp.apps.MainConfig'
Mine is main
- Create STATICFILES_DIRS ,that will connect the static files with the BASE_DIR.But that's not enough
- urls.py 🔗
Theurlpatterns
list routes URLs to views
- Import include, path from django.urls
- import settings from django.conf
- Import static from django.conf.urls.static
- Add
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
next to 'urlpatterns' list - Append to the list the path that to the views.py that is going to render your website, with functions or classes, is your choice.
- urls.py 🔗
Remember that:
It is a file that is created by the user and not by default
- Import views file
- Define urlspatterns as it will be connected with urls.py from testsite folder in order to render the html file
- Append the directory to views.py inside urlspatterns like that:path('',views.index,name='index')
- views.py 👀
It's the core of the project,extracting data from the json file and pass it in a dictionary to html file
Inside we have html file that is the main element for the front-end.
Check this thing also:
{% load static %}
is the first line of code inside the html file that loads all the static fileshref="{% static 'main/stylesheet.css' %}
it links the stylesheet.css file with the html This way the css file is connected with the help of these two lines of code,along with the STATICFILES_DIRS in settings.py and urls.py inside testsite folder