使用 django 模板 url 标签时,项目名称自动插入到 url 中

发布于 2024-08-19 22:05:54 字数 411 浏览 8 评论 0原文

我将“url”模板标签应用到当前 Django 项目中的所有链接。

我的网址命名如下...

url(r'^login/$', 'login', name='site_login'),

这允许我访问我网站根目录的 /login 。我的模板标签定义如下...

<a href="{% url site_login %}">

它工作正常,除了 Django 自动将该网址解析为 /myprojectname/login,而不是 /login。两个网址都可以访问。为什么?是否有删除项目名称的选项?所有 url 标签都会出现这种情况,而不仅仅是这个。

I am applying the 'url' template tag to all links in my current Django project.

I have my urls named like so...

url(r'^login/

This allows me to access /login at my site's root. I have my template tag defined like so...

<a href="{% url site_login %}">

It works fine, except that Django automatically resolves that url as /myprojectname/login, not /login. Both urls are accessible. Why? Is there an option to remove the projectname? This occurs for all url tags, not just this one.

, 'login', name='site_login'),

This allows me to access /login at my site's root. I have my template tag defined like so...

It works fine, except that Django automatically resolves that url as /myprojectname/login, not /login. Both urls are accessible. Why? Is there an option to remove the projectname? This occurs for all url tags, not just this one.

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

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

发布评论

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

评论(2

原谅过去的我 2024-08-26 22:05:54

显然,这是我的 apache2 配置的问题。我基本上从 django 网站复制了指令,做了一个小修改:

http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#basic-configuration

我进行了更改,以便我的 Django 项目将在我网站的根目录中提供服务。

我必须从 django.root 选项中删除“/mysite”。我的最终输出如下所示,在 django.root 之后没有任何值:

<Location "/"> 
    SetHandler python-program 
    PythonHandler django.core.handlers.modpython 
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings 
    PythonOption django.root 
    PythonDebug On 
</Location> 

根据记录,我正在运行:

Django 1.0
阿帕奇2.2
mod_python 3.3.1(我认为...:))
Ubuntu Hardy 8.04.4

从此处复制: http://groups.google.com /group/django-users/msg/5e79ed1e694a3776

Apparently, it was a problem with my apache2 configuration. I basically copied the directives from the django website, with one small modification:

http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#basic-configuration

I changed to so my Django project would be served at my site's root directory.

I had to remove '/mysite' from the django.root option. My final output looks like this, with no value after django.root:

<Location "/"> 
    SetHandler python-program 
    PythonHandler django.core.handlers.modpython 
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings 
    PythonOption django.root 
    PythonDebug On 
</Location> 

For the record, I am running:

Django 1.0
Apache 2.2
mod_python 3.3.1 (i think... :))
Ubuntu Hardy 8.04.4

Copied from here: http://groups.google.com/group/django-users/msg/5e79ed1e694a3776

听,心雨的声音 2024-08-26 22:05:54

在你的 url conf 中,django 比另一个更早地遇到这个 url 模式。

由于 url 解析是一一完成的,只需在主 urls.py 中包含之前在主登录中移动即可

In your url conf django encounters this url pattern earlier than the other one.

Since url resolution is done one by one, just move around the main login before the include within the main urls.py

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