我正在使用scaleway上的裸机服务器,我正在尝试从django教程编写第一个django应用程序。
由于我使用的是远程服务器,因此我无法使用manage.py startserver
而不是尝试使用wsgi.py。
这是我的apache conf文件:
WSGIScriptAlias /test-app /usr/local/django-apps/testApp/testApp/wsgi.py
WSGIPythonPath /usr/local/django-apps/testApp
<Directory /usr/local/django-apps/testApp/testApp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
以django-admin创建的项目为例。
如果我从控制台使用curl http://127.0.0.1:8000
,我可以看到“它有效!祝贺......”。
我将服务器IP(和127.0.0.1)添加到ALLOWED_HOST,这是我第一次尝试浏览http://myserverip/test-app
现在curl to loopback仍然有效,但浏览器通过公共IP报告:
Page not found (404)
Request Method: GET
Request URL: http://mypublicip/test-app/
Using the URLconf defined in testApp.urls, Django tried these URL patterns, in this order:
admin/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
有什么建议吗? 感谢。
@LaurynasTamulevičius:
TestApp - &gt; urls.py:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
我不明白为什么生成的html从runserver到wsgi不同。
在网上搜索,似乎manage.py startserver
自动为正确管理脚本和代码创建了一些别名。
一个别名应该是/static/
,指向django安装下的特定目录。其他别名也可以用同样的方式创建。
在我看来,这是一种简单的傻瓜方式,但它是一个了解部署django网站需要什么的perver系统。
@Graham Dumpleton
是。使用http://mypublicip/test-app/admin
,我可以看到没有css的管理登录页面。我认为对于css,需要/static/
别名来进行django管理。
如果我理解,我需要添加/
的句柄。
我怎么能这样做?在testApp.urls
添加内容?
使用runserver时为什么不需要?或者“它是否有效!祝贺...”页面仅使用runserver可见?
确定!我取得了第一个结果! 现在,对于管理页面,已加载css。
我添加/修改了一些行到settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/test-app/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/test-app/media/'
然后运行命令python3 manage.py collectstatic
。该命令在BASE_DIR中创建了目录 static ,包含所有必需的静态文件。
然后将一些行添加到apache2配置文件中,这些行指示apache2知道STATIC_URL和MEDIA_URL的别名并允许apache2到达目录。
#static files
Alias /test-app/media/ /usr/local/django-apps/testApp/media/
Alias /test-app/static/ /usr/local/django-apps/testApp/static/
<Directory /usr/local/django-apps/testApp/static>
Require all granted
</Directory>
<Directory /usr/local/django-apps/testApp/media>
Require all granted
</Directory>
此时我不知道:django runserver显示的默认页面“It work ...”是404页面的自动重定向,由runserver制作吗?
我希望在wsgi配置中看到一个“它工作......”,但也许我错了。
部署站点需要所有站点都使用默认页面和/
的重定向。
我现在可以继续工作了。