1

我坚持将我的 Django 项目迁移到生产服务器。这是情况。我的最终结果页面(HTML 源代码)如下所示:

Number of exponentials: 1 <br /><br />
Function: 98.919106*2.71828182845905**(-0.014026249*t) <br /><br />
Parameters of exponentials: {&#39;a&#39;: [98.919105999999999], &#39;k&#39;: [0.014026248999999999], &#39;c&#39;: [0]} <br /><br />
Non physical solution:  False <br /><br />

<img src="plots/data_2e_03.png" alt="some_text"/> 

它是一个由我的 Django 应用程序中的数据填充的模板。模板如下所示:

Number of exponentials: {{rp.user_choice.number_of_exponentials}} <br /><br />
Function: {{rp.user_choice.function}} <br /><br />
Parameters of exponentials: {{rp.user_choice.parameters_of_exponentials}} <br /><br />
Non physical solution:  {{rp.user_choice.non_physical_solution}} <br /><br />

<img src="plots/{{rp.input_file_name}}.png" alt="some_text"/> 

问题是图像没有显示。只有 alt 被打印出来。当我检查 html 源代码(粘贴在上面)时,它显示 plots/data_2e_03.png 作为链接(是的,在 HTML 源代码中)。当我单击它时,它会将我带到另一个页面的来源。

<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p> 
<p>Please contact the server administrator,
[no address given] and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
<hr>
<address>Apache/2.2.16 (Debian) Server at bioinformatika.djangoserver Port 80</address>
</body></html>

这是我的 apache 站点配置:

<VirtualHost *:80>

ServerName bioinformatika.djangoserver
DocumentRoot /home/user/bioinformatika/Server/bioinformatika/apache

Alias /plots/ /home/user/bioinformatika/Server/bioinformatika/bioinformatika/plots/

<Directory /home/user/bioinformatika/Server/bioinformatika/bioinformatika/plots>
Order deny,allow
Allow from all
</Directory>


<Directory /home/user/bioinformatika/Server/bioinformatika/apache>
    Order allow,deny
    Allow from all
</Directory>

WSGIDaemonProcess bioinformatika.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup bioinformatika.djangoserver

WSGIScriptAlias / /home/user/bioinformatika/Server/bioinformatika/apache/django.wsgi

为了让 apache 处理我的媒体文件,我添加了:

Alias /plots/ /home/user/bioinformatika/Server/bioinformatika/bioinformatika/plots/

<Directory /home/user/bioinformatika/Server/bioinformatika/bioinformatika/plots>
Order deny,allow
Allow from all
</Directory>

其他部分是关于设置 apache、mod_wsgi 和 Django,并且正在工作。

一旦我删除了 Django settings.py 中的调试。显示源的页面(带有内部服务器错误)现在已呈现并显示在浏览器中。/etc/log/apache2/error.log 给出以下内容:

[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]     return callback(request, **param_dict)
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]   File "/home/user/bioinformatika/virtual_bio/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/utils/decorators.py", line 91, in _wrapped_view
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]     response = view_func(request, *args, **kwargs)
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]   File "/home/user/bioinformatika/virtual_bio/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/views/defaults.py", line 32, in server_error
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]     t = loader.get_template(template_name) # You need to create a 500.html template.
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]   File "/home/user/bioinformatika/virtual_bio/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/template/loader.py", line 145, in get_template
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]     template, origin = find_template(template_name)
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]   File "/home/user/bioinformatika/virtual_bio/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/template/loader.py", line 138, in find_template
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14]     raise TemplateDoesNotExist(name)
[Sun Jun 24 11:22:15 2012] [error] [client 192.168.0.14] TemplateDoesNotExist: 500.html

任何建议表示赞赏。我认为奇怪的行为可能与我的服务器设置有关,而我的结果不是默认的,容易解决的问题,如果是这种情况,如果有人可以请检查我的 apache 配置是否正确,我将不胜感激。

更新:

这是我想念配置路径的问题。我不清楚 STATIC_URL、STATIC_ROOT、MEDIA_URL 和 MEDIA_ROOT 的含义,这导致了我无法完全诊断的部署问题中的问题。

但是,避免这种情况的方法是对如何在开发和生产服务器上提供文件有一个有效的概念性知识。

4

1 回答 1

4

在您的 projectname/templates 文件夹中,您需要创建一个名为 500.html 的文件,以便服务器在非调试模式下显示。(您应该为类似目的创建一个 404.html 文件)。

您还需要确保 settings.TEMPLATE_DIRS 包括服务器上您的 projectname/templates 文件夹的绝对路径。

于 2012-06-24T17:30:47.080 回答