【发布时间】:2009-11-26 01:55:51
【问题描述】:
我有一个 Django 项目,其中包含一个名为 pub 的应用程序。我正在尝试对其进行设置,以便我可以在顶级 urls.py 中包含来自每个应用程序的 urls.py(我会越来越多)。我还有一个模板,它使用“url”函数来解析视图上的 URL,在 openidgae 模块中定义。问题是,在 httprequest 被路由到 pub.views.index 之后(就像它应该的那样),我尝试通过渲染一个使用模板'url'函数的模板来响应。我在下面显示的代码也在这里:http://gist.github.com/243158
这是我的顶级 urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'', include('openidgae.urls')),
(r'^pub', include('pub.urls')),
)
和 pub/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'', 'pub.views.index'),
(r'^/$', 'pub.views.index'),
)
和模板/base.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>{% block title %}My amazing site{% endblock %}</title>
</head>
<body>
<div id="header">
{% if lip %}
Welcome {{ lip.pretty_openid }}
<a href="{% url openidgae.views.LogoutSubmit %}">logout</a>
{% else %}
<form id="login-form" action="{% url openidgae.views.OpenIDStartSubmit %}?continue={{continueUrl}}" method="post">
<input type="text" name="openid_identifier" id="openid_identifier" />
<input type="submit" value="Verify" />
</form>
<!-- BEGIN ID SELECTOR -->
<script type="text/javascript" id="__openidselector" src="https://www.idselector.com/selector/46b0e6d0c8ba5c8617f6f5b970865604c9f87da5" charset="utf-8"></script>
<!-- END ID SELECTOR -->
{% endif %}
</div>
{% block content %}{% endblock %}
</body>
</html>
和模板/pub/index.html:
{% extends "base.html" %}
{% block title %}blahblah!{% endblock %}
{% block content %}
blahblahblah
{% endblock %}
最后是 pub/views.py:
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms
import openidgae
def index(request):
lip = openidgae.get_current_person(request, HttpResponse())
resp = render_to_response('pub/index.html', {'lip': lip})
return resp
现在,如果我在顶级 urls.py 中将第二个模式设置为直接指向 'pub.views.index',那么一切正常,但如果我使用 include 函数则不会。
有什么想法吗?我确定问题与 urlpattern 有关,当 HttpRequest 由 pub 应用程序处理而不是由顶级,但我不明白为什么或如何解决它。
【问题讨论】:
标签: python django google-app-engine