|
| 1 | +--- |
| 2 | +title: Django Baseline Testing on Google Axion C4A Arm Virtual Machine |
| 3 | +weight: 5 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Django Baseline Testing on GCP SUSE VMs |
| 10 | +This baseline testing guide verifies that your **Django installation**, **web server**, and **basic application routing** are functioning correctly on a **Google Cloud SUSE Linux Arm64 (Axion C4A)** virtual machine. |
| 11 | +You will first run the Django development server and access it from your browser, then create a simple Django app to ensure routing works. |
| 12 | + |
| 13 | +### Baseline 1 — View Django Welcome Page |
| 14 | +This test confirms that Django is installed correctly and the server runs successfully. |
| 15 | + |
| 16 | +#### Activate your Python environment |
| 17 | +Before running Django, activate the Python virtual environment you created during installation. |
| 18 | + |
| 19 | +```console |
| 20 | +source venv/bin/activate |
| 21 | +``` |
| 22 | + |
| 23 | +#### Create a new Django project |
| 24 | +Run the following command to create a new Django project named `myproject`: |
| 25 | + |
| 26 | +```console |
| 27 | +django-admin startproject myproject |
| 28 | +cd myproject |
| 29 | +``` |
| 30 | + |
| 31 | +This generates the following directory structure: |
| 32 | + |
| 33 | +```markdown |
| 34 | +myproject/ |
| 35 | +├── manage.py |
| 36 | +└── myproject/ |
| 37 | + ├── settings.py |
| 38 | + ├── urls.py |
| 39 | + ├── asgi.py |
| 40 | + └── wsgi.py |
| 41 | +``` |
| 42 | +- `manage.py` is Django’s command-line utility for project management (running server, migrations, etc.). |
| 43 | +- The inner `myproject/` folder contains the core configuration files that define your project’s settings and URLs.- |
| 44 | + |
| 45 | +#### Run initial migrations |
| 46 | +Migrations prepare your project’s database by creating the required tables for Django’s internal apps (admin, authentication, etc.): |
| 47 | + |
| 48 | +```console |
| 49 | +python manage.py migrate |
| 50 | +``` |
| 51 | + |
| 52 | +#### Start the Django development server |
| 53 | +Before starting the Django development server, you must configure your ALLOWED_HOSTS setting to allow access from your VM’s external IP. |
| 54 | +This ensures that Django accepts HTTP requests from outside the localhost (e.g., when testing in a browser or from another machine). |
| 55 | + |
| 56 | +**ALLOWED_HOSTS:** is a security setting in Django that defines which host/domain names your Django site can serve. |
| 57 | + |
| 58 | +- Navigate to Your Project Settings |
| 59 | + Move into your Django project directory where the settings.py file is located. |
| 60 | + |
| 61 | + ```console |
| 62 | + cd ~/myproject/mysite/mysite |
| 63 | + ``` |
| 64 | + |
| 65 | +- Open settings.py File |
| 66 | + Use any text editor (like vi or nano) to open the file. |
| 67 | + |
| 68 | + ```console |
| 69 | + vi settings.py |
| 70 | + ``` |
| 71 | + |
| 72 | +- Locate the `ALLOWED_HOSTS` Line |
| 73 | + Inside the file, find the following line: |
| 74 | + |
| 75 | + ```python |
| 76 | + ALLOWED_HOSTS = [] |
| 77 | + ``` |
| 78 | + This setting defines which host/domain names Django will serve. |
| 79 | + |
| 80 | +- Allow All Hosts (for Testing Only) |
| 81 | + To make your Django app accessible from your VM’s external IP address, update it to: |
| 82 | + ```pthon |
| 83 | + ALLOWED_HOSTS = ['*'] |
| 84 | + ``` |
| 85 | +{{% notice Note %}} |
| 86 | +Allowing all hosts `('*')` is suitable **only for development or testing**. |
| 87 | +For production, replace `'*'` with specific domain names or IPs, such as: |
| 88 | +{{% /notice %}} |
| 89 | + |
| 90 | +```python |
| 91 | +ALLOWED_HOSTS = ['your-external-ip', 'your-domain.com'] |
| 92 | +``` |
| 93 | + |
| 94 | +#### Enable Port 8000 in GCP Firewall |
| 95 | + |
| 96 | +By default, Google Cloud VMs block external traffic on custom ports like 8000. You must open this port to access Django from your browser. |
| 97 | + |
| 98 | +**Now start the Django development server:** |
| 99 | + |
| 100 | +```console |
| 101 | +python manage.py runserver 0.0.0.0:8000 |
| 102 | +``` |
| 103 | + |
| 104 | +#### View in browser |
| 105 | +Open a web browser on your local machine (Chrome, Firefox, Edge, etc.) and enter the following URL in the address bar: |
| 106 | + |
| 107 | +```console |
| 108 | +http://<YOUR_VM_EXTERNAL_IP>:8000 |
| 109 | +``` |
| 110 | +- Replace `<YOUR_VM_EXTERNAL_IP>` with the public IP of your GCP VM. |
| 111 | + |
| 112 | +If everything is set up correctly, you should see the default Django welcome page (“The install worked successfully!”). It looks like this: |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +### Baseline 2 — Create a Simple Django App |
| 117 | +This test ensures Django’s application routing and view rendering work as expected. |
| 118 | + |
| 119 | +#### Stop the server |
| 120 | +Press `Ctrl + C` to stop the Django server if running. |
| 121 | + |
| 122 | +#### Create a new app |
| 123 | +Within your Django project directory, create a new app named `hello`: |
| 124 | + |
| 125 | +```console |
| 126 | +python manage.py startapp hello |
| 127 | +``` |
| 128 | + |
| 129 | +**This creates the following directory:** |
| 130 | + |
| 131 | +```markdown |
| 132 | +hello/ |
| 133 | +├── admin.py |
| 134 | +├── apps.py |
| 135 | +├── models.py |
| 136 | +├── tests.py |
| 137 | +├── views.py |
| 138 | +└── urls.py |
| 139 | +``` |
| 140 | + |
| 141 | +#### Create a simple view |
| 142 | +Edit `hello/views.py`. Replace your existing file with this: |
| 143 | + |
| 144 | +```python |
| 145 | +from django.http import HttpResponse |
| 146 | + |
| 147 | +def home(request): |
| 148 | + return HttpResponse("<h1>Hello, Django on GCP SUSE ARM64!</h1>") |
| 149 | +``` |
| 150 | +This defines a simple view function that sends a basic HTML message as the HTTP response. |
| 151 | + |
| 152 | +#### Create app URL configuration |
| 153 | +Create a new file hello/urls.py and add: |
| 154 | + |
| 155 | +```python |
| 156 | +from django.urls import path |
| 157 | +from . import views |
| 158 | + |
| 159 | +urlpatterns = [ |
| 160 | + path('', views.home, name='home'), |
| 161 | +] |
| 162 | +``` |
| 163 | +This maps the root URL `(/)`of your app to the `home()` view function. |
| 164 | + |
| 165 | +#### Link the app to the main project |
| 166 | +Replace your default `myproject/urls.py` file with this version. |
| 167 | + |
| 168 | +```python |
| 169 | +"""myproject URL Configuration |
| 170 | +
|
| 171 | +The `urlpatterns` list routes URLs to views. For more information please see: |
| 172 | + https://docs.djangoproject.com/en/3.2/topics/http/urls/ |
| 173 | +Examples: |
| 174 | +Function views |
| 175 | + 1. Add an import: from my_app import views |
| 176 | + 2. Add a URL to urlpatterns: path('', views.home, name='home') |
| 177 | +Class-based views |
| 178 | + 1. Add an import: from other_app.views import Home |
| 179 | + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') |
| 180 | +Including another URLconf |
| 181 | + 1. Import the include() function: from django.urls import include, path |
| 182 | + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) |
| 183 | +""" |
| 184 | + |
| 185 | +from django.contrib import admin |
| 186 | +from django.urls import path, include |
| 187 | + |
| 188 | +urlpatterns = [ |
| 189 | + path('admin/', admin.site.urls), |
| 190 | + path('', include('hello.urls')), |
| 191 | +] |
| 192 | +``` |
| 193 | +This tells Django to delegate routing for the root path (`''`) to the `hello` app’s URLs. |
| 194 | + |
| 195 | +#### Add the app to settings |
| 196 | +This makes Django aware of your new app so it can load its configuration and routes. |
| 197 | +Edit `myproject/settings.py` → add `'hello'` to INSTALLED_APPS: |
| 198 | + |
| 199 | +```python |
| 200 | +INSTALLED_APPS = [ |
| 201 | + 'django.contrib.admin', |
| 202 | + 'django.contrib.auth', |
| 203 | + 'django.contrib.contenttypes', |
| 204 | + 'django.contrib.sessions', |
| 205 | + 'django.contrib.messages', |
| 206 | + 'django.contrib.staticfiles', |
| 207 | + 'hello', |
| 208 | +] |
| 209 | +``` |
| 210 | +#### Run the server again |
| 211 | + |
| 212 | +```console |
| 213 | +python manage.py runserver 0.0.0.0:8000 |
| 214 | +``` |
| 215 | + |
| 216 | +#### Test your app |
| 217 | +Open in browser: |
| 218 | + |
| 219 | +```console |
| 220 | +http://<YOUR_VM_IP>:8000 |
| 221 | +``` |
| 222 | +You should see the Django app. It looks like this: |
| 223 | + |
| 224 | + |
0 commit comments