Pamela Fox commited on
Commit
74f4314
·
1 Parent(s): f85cf8e

Changes for Azure deployment

Browse files
.env ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ FLASK_ENV=development
2
+ DBNAME=quizsite
3
+ DBHOST=localhost
4
+ DBUSER=pamelafox
5
+ DBPASS=
.vscode/settings.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "appService.defaultWebAppToDeploy": "/subscriptions/74feb6f3-f646-478d-a99b-37fafee75e29/microsoft.web/sites/subscriptions/74feb6f3-f646-478d-a99b-37fafee75e29/resourceGroups/django-example-quizsite/providers/Microsoft.Web/sites/django-example-quizsite",
3
+ "appService.deploySubpath": "."
4
+ }
manage.py CHANGED
@@ -3,10 +3,22 @@
3
  import os
4
  import sys
5
 
 
 
6
 
7
  def main():
8
  """Run administrative tasks."""
9
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quizsite.settings")
 
 
 
 
 
 
 
 
 
 
10
  try:
11
  from django.core.management import execute_from_command_line
12
  except ImportError as exc:
@@ -18,5 +30,5 @@ def main():
18
  execute_from_command_line(sys.argv)
19
 
20
 
21
- if __name__ == "__main__":
22
- main()
 
3
  import os
4
  import sys
5
 
6
+ from dotenv import load_dotenv
7
+
8
 
9
  def main():
10
  """Run administrative tasks."""
11
+ # If WEBSITE_HOSTNAME is defined as an environment variable, then we're running on Azure App Service
12
+
13
+ # Only for Local Development - Load environment variables from the .env file
14
+ if not 'WEBSITE_HOSTNAME' in os.environ:
15
+ print("Loading environment variables for .env file")
16
+ load_dotenv('./.env')
17
+
18
+ # When running on Azure App Service you should use the production settings.
19
+ settings_module = "quizsite.production" if 'WEBSITE_HOSTNAME' in os.environ else 'quizsite.settings'
20
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
21
+
22
  try:
23
  from django.core.management import execute_from_command_line
24
  except ImportError as exc:
 
30
  execute_from_command_line(sys.argv)
31
 
32
 
33
+ if __name__ == '__main__':
34
+ main()
quizsite/production.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .settings import *
2
+ import os
3
+
4
+ # Configure the domain name using the environment variable
5
+ # that Azure automatically creates for us.
6
+ ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
7
+ CSRF_TRUSTED_ORIGINS = ['https://'+ os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
8
+ DEBUG = False
9
+
10
+ # DBHOST is only the server name, not the full URL
11
+ hostname = os.environ['DBHOST']
12
+
13
+ # Configure Postgres database; the full username for PostgreSQL flexible server is
14
+ # username (not @sever-name).
15
+ DATABASES = {
16
+ 'default': {
17
+ 'ENGINE': 'django.db.backends.postgresql',
18
+ 'NAME': os.environ['DBNAME'],
19
+ 'HOST': hostname + ".postgres.database.azure.com",
20
+ 'USER': os.environ['DBUSER'],
21
+ 'PASSWORD': os.environ['DBPASS']
22
+ }
23
+ }
quizsite/settings.py CHANGED
@@ -11,6 +11,8 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
11
  """
12
 
13
  from pathlib import Path
 
 
14
 
15
  # Build paths inside the project like this: BASE_DIR / 'subdir'.
16
  BASE_DIR = Path(__file__).resolve().parent.parent
@@ -77,11 +79,10 @@ WSGI_APPLICATION = "quizsite.wsgi.application"
77
  DATABASES = {
78
  "default": {
79
  "ENGINE": "django.db.backends.postgresql_psycopg2",
80
- "NAME": "quizsite",
81
- "USER": "pamelafox",
82
- "PASSWORD": "",
83
- "HOST": "localhost",
84
- "PORT": "",
85
  }
86
  }
87
 
 
11
  """
12
 
13
  from pathlib import Path
14
+ import os
15
+
16
 
17
  # Build paths inside the project like this: BASE_DIR / 'subdir'.
18
  BASE_DIR = Path(__file__).resolve().parent.parent
 
79
  DATABASES = {
80
  "default": {
81
  "ENGINE": "django.db.backends.postgresql_psycopg2",
82
+ 'NAME': os.environ['DBNAME'],
83
+ 'HOST': os.environ['DBHOST'],
84
+ 'USER': os.environ['DBUSER'],
85
+ 'PASSWORD': os.environ['DBPASS']
 
86
  }
87
  }
88
 
quizsite/wsgi.py CHANGED
@@ -11,6 +11,7 @@ import os
11
 
12
  from django.core.wsgi import get_wsgi_application
13
 
14
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quizsite.settings")
 
15
 
16
  application = get_wsgi_application()
 
11
 
12
  from django.core.wsgi import get_wsgi_application
13
 
14
+ settings_module = 'quizsite.production' if 'WEBSITE_HOSTNAME' in os.environ else 'quizsite.settings'
15
+ os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
16
 
17
  application = get_wsgi_application()
quizzes/templates/quizzes/index.html CHANGED
@@ -4,16 +4,21 @@
4
  <meta charset="utf-8">
5
  <meta name="viewport" content="width=device-width">
6
  <title>Quizzes</title>
 
7
  </head>
8
  <body>
 
 
9
  {% if quiz_list %}
10
  <ul>
11
  {% for quiz in quiz_list %}
12
- <li><a href="{% url 'quizzes:display' quiz.id %}">{{ quiz.name }}</a></li>
13
  {% endfor %}
14
  </ul>
15
  {% else %}
16
  <p>No quizzes are available.</p>
17
  {% endif %}
 
 
18
  </body>
19
  </html>
 
4
  <meta charset="utf-8">
5
  <meta name="viewport" content="width=device-width">
6
  <title>Quizzes</title>
7
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
8
  </head>
9
  <body>
10
+ <div class="container">
11
+ <h1>Quizzes</h1>
12
  {% if quiz_list %}
13
  <ul>
14
  {% for quiz in quiz_list %}
15
+ <li><a href="{% url 'quizzes:display_quiz' quiz.id %}">{{ quiz.name }}</a></li>
16
  {% endfor %}
17
  </ul>
18
  {% else %}
19
  <p>No quizzes are available.</p>
20
  {% endif %}
21
+ </div>
22
+
23
  </body>
24
  </html>
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  Django==4.1.1
2
- psycopg2
 
 
1
  Django==4.1.1
2
+ psycopg2
3
+ python-dotenv