Skip to content

Instantly share code, notes, and snippets.

@godfather68
Created May 11, 2020 00:11
Show Gist options
  • Save godfather68/dee98584040f69bac74477b05e783f62 to your computer and use it in GitHub Desktop.
Save godfather68/dee98584040f69bac74477b05e783f62 to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand
import os #
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
class Command(BaseCommand):
help = "Templates Generator"
template_dir = os.path.join(BASE_DIR, 'templates')
file_path = os.path.join(template_dir, '_base.html')
def handle(self, *args, **kwrags):
self.stdout.write("Generating _base.html template ...")
self.stdout.write('----------------------------------')
def directory_checker(self):
# Checking if the directory exists
if os.path.exists(self.template_dir):
self.stdout.write("Checking if 'templates' directory exists ... Done")
return self.template_dir
else:
self.stdout.write("Checking if 'templates' directory exists ... Missing")
self.stdout.write("Creating templates directory")
os.makedirs(self.template_dir)
self.directory_checker()
def base_file_checker(self):
self.directory_checker()
# creating the file & adding the base html content
if not os.path.isfile(self.file_path):
content = """
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}{% endblock %}</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!-- CSS -->
<link rel="stylesheet" href="">
<!-- JS -->
<script type="text/javascript"></script>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
"""
file = open(self.file_path, 'w')
file.write(content)
file.close()
self.stdout.write('\n')
self.stdout.write(self.style.SUCCESS('----------------------------------'))
self.stdout.write(self.style.SUCCESS('Base template created successfully'))
else:
self.stdout.write('\n')
self.stdout.write(self.style.SUCCESS('--------------------------------'))
self.stdout.write(self.style.SUCCESS("File exists. You're good to go !"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment