Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

html template reloading#32

Unanswered
aaronsgithub asked this question inQ&A
Jan 11, 2023· 2 comments· 3 replies
Discussion options

Great work on this Nick.

I just want to check if my understanding of html template reloading is correct.

With the normal django development server, setting theDEBUG flag in theTEMPLATES dict insettings.py allows changes to templates to be loaded in the browser when the browser is refreshed.

However with gunicorn as the server, this does not work.

TEMPLATES = [    {        "BACKEND": "django.template.backends.django.DjangoTemplates",        "DIRS": [os.path.join(BASE_DIR, "templates")],        "APP_DIRS": True,        "OPTIONS": {            "context_processors": [                "django.template.context_processors.debug",                "django.template.context_processors.request",                "django.contrib.auth.context_processors.auth",                "django.contrib.messages.context_processors.messages",            ],            "debug": DEBUG,    # <- this has no effect        },    },

Is this the expected behaviour or am I doing something wrong?

The only way I've managed to get templates to reload is by adding the following togunicorn.py (as per the docshere):

from django.conf import settings...reload_extra_files = [str(settings.BASE_DIR / "<app-name>" / "templates" / "<app-name>" / "<template-name>.html")]

And so, to get the browser to load changes to any template file, I would either have to explicitly list those templates inreload_extra_files or I could probably populate this list programmatically.

There is apull request for gunicorn that would make this simpler, but unfortunately it has been sitting unmerged for 4 years.

Is there a better way?

You must be logged in to vote

Replies: 2 comments 3 replies

Comment options

Hi,

We pass our app into gunicorn and then gunicorn is configured with:

reload=bool(strtobool(os.getenv("WEB_RELOAD","false")))

That should handle all reloading by itself. It does for this example app which has a custompages app with atemplates/pages/home.html file. When that file changes, you can reload your browser and see it without customizing anything else.

You must be logged in to vote
0 replies
Comment options

Thanks for the quick reply.

I did see that setting, however for me changes to the templates to not appear in the browser after reload.

I've just cloned the repository and checked this in the pages app.

Two things suggested to me that we shouldn't expect the reload to work:

  1. The gunicorn docs say to use thereload_extra_files "to also watch and reload on additional files (e.g., templates, configurations, specifications, etc.)" which would suggest the reload setting alone is not enough for template files.

  2. Thepull request on the gunicorn repo with users suggesting the same as 1.

But again I'm not certain if this is the case or if the templates should reload on change and there is something wrong with my setup.

Have you been able to confirm the template reloading works recently?


Edit

So I'm developing on Ubuntu 22.04 on WSL2 on Windows 11.

If I try to edit a template from WSL then as I wrote above the templates do not reload.

However, if I edit inside adevcontainer i.e. insider the docker container itself, the change is detected and gunicorn reloads so I guess in my setup, gunicorn is not seeing the file system changes originating from the WSL bind mount.

In any case, I like to develop inside the devcontainer so its not an issue for me.

You must be logged in to vote
3 replies
@nickjj
Comment options

I'm using Ubuntu 22.04 and WSL 2 too, but on Windows 10.

Are your files in your WSL 2 file system? I think it might not work if your files are in a Windows drive that you're mounting into WSL 2. In my case my files are in WSL 2's file system.

@aaronsgithub
Comment options

The files are in the WSL2 file system.

Looking atone of the old issue threads which looks similar, it could be that I'm using neovim and running into the same issue which that affected user identified.

@nickjj
Comment options

It's possible but that would potentially only apply to CSS files due to a bug in Tailwind and you mentioned Django templates were causing an issue with not being reloaded. It's worth a shot delving down that rabbit hole tho or at least seeing if it happens with other editors as a quick test.

I use Vim too btw. My vimrc is at:https://github.com/nickjj/dotfiles/blob/master/.vimrc

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@aaronsgithub@nickjj

[8]ページ先頭

©2009-2025 Movatter.jp