Python url 模板标签只给出绝对 url 的一部分.. ..一定是缺乏睡眠

发布于 2024-08-19 01:16:18 字数 1418 浏览 3 评论 0原文

我一定是严重缺乏睡眠,但我很困惑。我似乎无法弄清楚如何获取 {% url %} 指令来为我提供正确的网址。因此,让我们从基础知识开始......

urls.py

from people.views import employee_detail

urlpatterns = patterns("",
    url(r'/uid/(?P<id>[0-9]+)/$', employee_detail, {'template_name' : 'people/single.html'}, name = 'employee_view'),
)

views.py

from django.shortcuts import render_to_response, get_list_or_404
from django.template import RequestContext
from people.models import Employee, OfficeLocation

def employee_detail(request, id, template_name = None):
    """
        This will give you a full detailed information on a user
        based off of there name.
    """
    person = Employee.objects.get(id = id)        
    return render_to_response(template_name, _getDetail(person),
                              context_instance = RequestContext(request))

最后,这是我的 people/single.html 的示例片段。

people/single.html


主管:{{主管}}

现在我可以看到我正在来回传递正确的数据。例如这个结果 在代码中看起来像

Supervisor: NAME< 的链接中/code>

现在我做错了什么..我缺少网址的主机名部分..有人可以告诉我如何返回“http://127.0.0.1:8000/uid/415" 或者任何主机名是什么?

Grr.. 一定是简单,我知道我患有睡眠不足..

I must be suffering from a serious lack of sleep but I am stumped. I can't seem to figure out how to get the {% url %} directive to give me the right url. So let's start with the basics..

urls.py

from people.views import employee_detail

urlpatterns = patterns("",
    url(r'/uid/(?P<id>[0-9]+)/

views.py

from django.shortcuts import render_to_response, get_list_or_404
from django.template import RequestContext
from people.models import Employee, OfficeLocation

def employee_detail(request, id, template_name = None):
    """
        This will give you a full detailed information on a user
        based off of there name.
    """
    person = Employee.objects.get(id = id)        
    return render_to_response(template_name, _getDetail(person),
                              context_instance = RequestContext(request))

Lastly here is a sample snippet of my people/single.html.

people/single.html

<tr>
<td width="300px">Supervisor: <a href="{% url employee_view , id=supervisor_id %}">{{ supervisor }}</a></td>
</tr>

Now I can see that I am passing the right data back and forth. For example this results
in a link which in the code looks like

<td width="300px">Supervisor: <a href="//uid/415/">NAME</a></td>

Now what am I doing wrong.. I'm missing the hostname part of the url.. Can someone please tell me how to get back "http://127.0.0.1:8000/uid/415" or whatever the hostname is?

Grr.. It's got to be simple I know I'm suffering from a lac of sleep..

, employee_detail, {'template_name' : 'people/single.html'}, name = 'employee_view'), )

views.py

Lastly here is a sample snippet of my people/single.html.

people/single.html

<tr>
<td width="300px">Supervisor: <a href="{% url employee_view , id=supervisor_id %}">{{ supervisor }}</a></td>
</tr>

Now I can see that I am passing the right data back and forth. For example this results
in a link which in the code looks like

<td width="300px">Supervisor: <a href="//uid/415/">NAME</a></td>

Now what am I doing wrong.. I'm missing the hostname part of the url.. Can someone please tell me how to get back "http://127.0.0.1:8000/uid/415" or whatever the hostname is?

Grr.. It's got to be simple I know I'm suffering from a lac of sleep..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-08-26 01:16:19

get_absolute_url 是一个用词不当,并且永远不会返回绝对(方案+主机名+端口)部分。对于这方面的事情,您需要使用 站点框架 (使用 站点.objects.get_current()) 并分别获取域等。

该方法的命名引起了足够的关注,Simon Willison 提议完全替换它

get_absolute_url is a misnomer, and never returns an absolute (scheme + hostname + port) part. For that aspect of things, you need to use the Sites framework (using Site.objects.get_current()) and get the domain, etc separately.

The naming of the method has raised enough eyebrows that Simon Willison has proposed replacing it entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文