在重新启动UWSGI之前,Django视图中的更改不会反映到页面

发布于 2025-02-06 10:57:26 字数 804 浏览 2 评论 0原文

我安装了django + uwsgi + nginx。 项目正在运行。但是,当我更改view中的内容时,在我重新启动uwsgi之前,更改不会反映到页面。我是否应该在每次更改view中重新启动uwsgi? 但是,当我将时间添加到view以在页面中显示。每次我刷新页面时,显示的时间都在更改。

我的视图是:

from django.shortcuts import render
from django.http import HttpResponse   # added
from django.utils import timezone
def home(request):
    return HttpResponse('This is the home page. 101' + str(timezone.now()))

我的urls.py

from django.contrib import admin
from django.urls import path
from godentiapp import views    # added 

urlpatterns = [
    path('', views.home, name='home'),    # added
    path('admin/', admin.site.urls),  
]

I installed Django + Uwsgi + Nginx.
Project is running. But when i change something in the view, the change is not reflected to page until i restart uwsgi. Should i restart uwsgi everytime i make a change in the view?
But when i add time to view to show in the page. The displayed time is changing everytime i refresh the page.

My view is :

from django.shortcuts import render
from django.http import HttpResponse   # added
from django.utils import timezone
def home(request):
    return HttpResponse('This is the home page. 101' + str(timezone.now()))

My urls.py :

from django.contrib import admin
from django.urls import path
from godentiapp import views    # added 

urlpatterns = [
    path('', views.home, name='home'),    # added
    path('admin/', admin.site.urls),  
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最佳男配角 2025-02-13 10:57:27

[i]应该每次[i]在视图中进行更改吗?

,每次更改源代码时,您都需要重新启动Web服务器,因为该文件始终加载一次。 Python解释器将读取源文件,并将其加载到内存中。更改文件将不会反映。

如果您使用开发模式中的Django ,对于Sourcde文件进行修改,它将自动 restart 服务器  [django-doc]

开发服务器根据需要自动重新加载Python代码。您无需重新启动服务器以进行代码更改即可生效。但是,添加文件之类的某些操作不会触发重新启动,因此在这些情况下您必须重新启动服务器。

但这不是在生产中进行的,不应进行,因为这可能会导致安全风险。

Should [I] restart uwsgi everytime [I] make a change in the view?

Yes, each time you change the source code, you need to restart the webserver, since the files are always loaded once. The Python interpreter will read the source file, and load it in memory. Changes the the file will not be reflected.

If you work with Django in development mode, for modifications to the sourcde file it will automatically restart the server [Django-doc]:

The development server automatically reloads Python code for each request, as needed. You don’t need to restart the server for code changes to take effect. However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.

But this is thus not done in production, and should not be done, since this can result in security risks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文