将Django迁移到生产服务器

时间:2012-06-24 16:30:53

标签: django

我坚持将我的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中的Debug。显示其源的页面(带有内部服务器错误)现在呈现并显示在浏览器中。 /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的含义对我来说不清楚,这导致我无法完全诊断出部署问题中的问题。

但是,避免它的方法是掌握如何在开发和生产服务器上提供文件的工作概念知识。

1 个答案:

答案 0 :(得分:4)

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

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