|
| 1 | +import requests |
1 | 2 | from django.template import TemplateDoesNotExist |
2 | | -from django.template.backends.django import DjangoTemplates |
3 | | -from django.template.backends.django import reraise, Template as BaseTemplate |
| 3 | +from django.template.backends.django import ( |
| 4 | + DjangoTemplates, |
| 5 | + reraise, |
| 6 | + Template as BaseTemplate, |
| 7 | +) |
| 8 | +from django.template import Template |
| 9 | + |
4 | 10 | from django.conf import settings |
5 | | -from urllib.parse import urlencode |
6 | 11 |
|
7 | 12 |
|
8 | 13 | class CustomAdminDjangoTemplate(DjangoTemplates): |
9 | 14 | def get_template(self, template_name): |
10 | | - if not template_name.startswith("astro/"): |
| 15 | + if not template_name.startswith("astro/") or not settings.DEBUG: |
11 | 16 | return super().get_template(template_name) |
12 | 17 |
|
13 | | - astro_path = template_name.split("/")[1].replace(".html", "") |
14 | | - |
15 | | - if settings.DEBUG: |
16 | | - template_name = "admin/iframe.html" |
| 18 | + # Proxy the request to Astro |
| 19 | + response = requests.get(f"http://127.0.0.1:8000/{template_name}") |
17 | 20 |
|
18 | 21 | try: |
19 | | - return Template( |
20 | | - template=self.engine.get_template(template_name), |
21 | | - backend=self, |
22 | | - astro_path=astro_path, |
23 | | - ) |
| 22 | + return BaseTemplate(Template(response.text), backend=self) |
24 | 23 | except TemplateDoesNotExist as exc: |
25 | 24 | reraise(exc, self) |
26 | | - |
27 | | - |
28 | | -class Template(BaseTemplate): |
29 | | - def __init__(self, astro_path: str, *args, **kwargs) -> None: |
30 | | - super().__init__(*args, **kwargs) |
31 | | - self.astro_path = astro_path |
32 | | - |
33 | | - def render(self, context=None, request=None): |
34 | | - context = { |
35 | | - "ASTRO_PATH": self.astro_path, |
36 | | - "ASTRO_URL_ARGS": urlencode({**context.get("arguments", {})}), |
37 | | - **context.get("arguments", {}), |
38 | | - } |
39 | | - return super().render(context=context, request=request) |
0 commit comments