django使用Apache时出错&;mod_wsgi
嘿,正如你们中的一些人所建议的,我一直在对django开发环境进行一些更改。 到目前为止,我已经成功地用postgres配置并运行了它 现在我尝试使用apache2和mod_wsgi运行这个应用程序,但是在遵循django文档的指导原则之后,我遇到了这个小问题 当我访问localhost/myapp/tasks时,此错误会引发:django使用Apache时出错&;mod_wsgi,django,apache,mod-wsgi,Django,Apache,Mod Wsgi,嘿,正如你们中的一些人所建议的,我一直在对django开发环境进行一些更改。 到目前为止,我已经成功地用postgres配置并运行了它 现在我尝试使用apache2和mod_wsgi运行这个应用程序,但是在遵循django文档的指导原则之后,我遇到了这个小问题 当我访问localhost/myapp/tasks时,此错误会引发: Request Method: GET Request URL: http://localhost/myapp/tasks/ Exception Type
Request Method: GET
Request URL: http://localhost/myapp/tasks/
Exception Type: TemplateSyntaxError
Exception Value:
Caught an exception while rendering: argument 1 must be a string or unicode object
Original Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py", line 71, in render_node
result = node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/defaulttags.py", line 126, in render
len_values = len(values)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 81, in __len__
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 238, in iterator
for row in self.query.results_iter():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 287, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 2369, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 19, in execute
return self.cursor.execute(sql, params)
TypeError: argument 1 must be a string or unicode object
... ... ...
然后它突出显示了一个{%fort in tasks%}模板标记,就像问题的根源在那里一样,但它在内置服务器上运行良好
与该页面关联的视图非常简单,只需获取所有任务对象。模板只是将它们显示在一个表上
此外,某些页面呈现正常。
我不想用代码来回答这个问题,所以如果你需要更多的信息,我很乐意提供。谢谢
编辑
因此,我的观点如下:
@login_required
def tasks(request, msg=''):
tasks = Task.objects.all()
message = msg
return custom_render('user/tasks.html',
{'tasks': tasks, 'message':message},
request)
这是我的模板:
{% block main_content %}
{% if message %}
<p id="message" class="info">
{{message}}
</p>
{% endif %}
<a href="{% url GProject.myapp.views.new_task %}">Nueva Tarea</a>
<table id="tasks-table" >
<thead>
<tr>
<th colspan="4" >{{tasks|length}} tareas pendientes</th>
</tr>
<tr>
<th>#</th>
<th>Proyecto</th>
<th>Título</th>
<th>Estado</th>
</tr>
</thead>
<tbody>
{% for t in tasks %}
<tr id="row-{{t.id}}" class="{% cycle 'row-0' 'row-1' %} priority-{{ t.priority }}">
<td width="25">
<a href="{% url GProject.myapp.views.view_task t.id %}">{{t.id}}</a>
</td>
<td>
<a href="{% url GProject.myapp.views.view_task t.id %}">{{t.project}}</a>
</td>
<td width="400">
<a href="{% url GProject.myapp.views.view_task t.id %}">
{{t.title}}
</a>
</td>
<td>{{t.get_status_display}}</td>
</tr>
{% empty %}
<tr><td>No tasks</td></tr>
{% endfor %}
</tbody>
</table>
{% endblock main_content %}
编辑
任务模型如下所示:
class Task(models.Model):
project = models.ForeignKey(Project)
title = models.CharField(max_length=128)
description = models.TextField(max_length=1500)
effort = models.IntegerField(null=True, blank=True)
priority = models.IntegerField(max_length=1, null=True, blank=True, choices=PRIORITY_VALUES)
severity = models.IntegerField(max_length=1, null=True, blank=True, choices=SEVERITY_VALUES)
asignee = models.ForeignKey(User, blank=True, null=True, related_name='asignee')
milestone = models.ForeignKey(Milestone, blank=True, null=True)
created_by = models.ForeignKey(User, blank=True, null=True, related_name='created_by')
status = models.IntegerField(max_length=1, choices=STATUS_VALUES, default=1)
resolution_comment = models.CharField(max_length=1500, null=True, blank=True) #comentario al resolver la task
due_date = models.DateField(blank=True, null=True)
created_on = models.DateTimeField(auto_now_add = True)
#print
def __unicode__(self):
return self.title
自定义渲染:
def custom_render(template_name, data_dict, request):
return render_to_response(template_name,
data_dict,
context_instance=RequestContext(request))
如果您在运行manage.py runserver时没有遇到问题,并且在使用mod_wsgi时也遇到了问题,那么我将查看数据库权限 首先,运行mod_wsgi的用户是否与运行python manage.py runserver的用户相同?如果没有,您需要授予mod_wsgi用户权限。作为一个示例,请尝试以下内容,其中apache是运行mod_wsgi的用户的名称:
sudo su - postgres
createuser -S -D -R apache
psql
< should now be in the psql shell >
grant all on database < your database name > to apache;
sudo su-postgres
createuser-S-D-R apache
psql
<现在应该在psql shell中>
将数据库<您的数据库名称>上的所有内容授予apache;
如果您在运行manage.py runserver时没有遇到问题,并且您在使用mod_wsgi时确实遇到了问题,那么我将查看数据库权限
首先,运行mod_wsgi的用户是否与运行python manage.py runserver的用户相同?如果没有,您需要授予mod_wsgi用户权限。作为一个示例,请尝试以下内容,其中apache是运行mod_wsgi的用户的名称:
sudo su - postgres
createuser -S -D -R apache
psql
< should now be in the psql shell >
grant all on database < your database name > to apache;
sudo su-postgres
createuser-S-D-R apache
psql
<现在应该在psql shell中>
将数据库<您的数据库名称>上的所有内容授予apache;
能否修改django源代码并打印sql和params的报告?当代码在devserver和apachePiotr下运行时,我真的不知道怎么做..Django并不总是指出模板中的正确行。不要太信任它。将DEBUG\u PROPAGATE\u EXCEPTIONS=True放入settings.py文件并重新加载页面,您应该会看到原始异常。还有post视图和模板代码。该错误与apache或mod_wsgi无关,它是一个TemplateSyntaxError。您正在使用自定义模板标签吗?能否提供模板?能否修改django源代码并打印sql和params的报告?当代码在devserver和apachePiotr下运行时,我真的不知道怎么做..Django并不总是指出模板中的正确行。不要太信任它。将DEBUG\u PROPAGATE\u EXCEPTIONS=True放入settings.py文件并重新加载页面,您应该会看到原始异常。还有post视图和模板代码。该错误与apache或mod_wsgi无关,它是一个TemplateSyntaxError。您正在使用自定义模板标签吗?你能提供模板吗?还认为它可能与数据库相关,但这不起作用。还认为它可能与数据库相关,但这不起作用。