Import Django template and static folders in settings.py file .
How to create a django template.?
- Create a templates folder in your directory.
- Create a file index.html (optional name) and create text for demo purpose.
- Go to views.py in django-app and render the html file ex: return render(request,’index.html’)
- Make sure about the mapping in the urls to views.
- Go to your django project settings file( django project folder = django-admin startproject <django-project-folder>)
- Create a variable TEMPLATE_DIR = <path-of-the-templates-folder>
- Add the variable TEMPLATE_DIR to DIRS array in TEMPLATES array in settings.py file. (TEMPLATES array is already created in settings.py by django)
- Runserver to see the text mentioned in the index.html
How to create a static images.?
- Create a folder static
- Create a folder images in static folder
- Save image file(jpg, png) in images folder
- Go to settings.py and create a new variable with static folder path ex: STATIC_DIR = <path-to-static-folder>
- Got to end of the setting.py file and under the STATIC_URL create an array and add STATIC variable (STATIC_URL is already created by django)
- Example STATICFILES_DIRS = [ STATIC_DIR,]
- Now run the server and you can see the image in the url
- http://127.0.0.1:8080/static/images/<image-file-name>
- Insert in html file, insert {% load staticfiles %} at the top of html page under <!DOCTYPE html> tag
- The use this code {% static ‘images/ss.png’ %} where you need the image
- Example: <img src=”{% static ‘images/ss.png’ %}” alt=””>
- You can see the image in the html file when server is up.