简体   繁体   English

Django反向查找呈现相同(错误)模板

[英]Django reverse lookup rendering the same (wrong) template

I have a small resume site that is not rendering a different template upon hitting a reversed URL. 我有一个小型的简历网站,当点击反向URL时不会呈现其他模板。

site/scripts/templates/scripts/index.html: 网站/脚本/模板/脚本/index.html:

<p>were at main</p>

<a href="{% url 'scripts:python' %}">python</a>
<br/>
<a href="{% url 'scripts:bash' %}">bash</a>

These links 'python' and 'bash' work in the URL bar, they take us to localhost:scripts/bash/ and localhost:scripts/python/, but the exact same webpage is displayed (index.html, or localhost:scripts/) 这些链接“ python”和“ bash”在URL栏中起作用,它们将我们带到localhost:scripts / bash /和localhost:scripts / python /,但显示的是完全相同的网页(index.html或localhost:scripts / )

site/scripts/urls.py: site / scripts / urls.py:

from django.conf.urls import patterns, include, url
from scripts import views


urlpatterns = patterns('',
    url(r'$',            views.index,           name='index'),
    url(r'python/$',     views.access_python,   name='python'),
    url(r'bash/$',       views.access_bash,     name='bash'),
)

site/scripts/views.py: site / scripts / views.py:

from django.shortcuts import render

def index(request):
    return render(request, 'scripts/index.html')

def access_python(request):
    return render(request, 'scripts/python.html')

def access_bash(request):
    return render(request, 'scripts/bash.html')

site/urls.py (main folder w/ settings.py): site / urls.py(主文件夹w / settings.py):

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'scripts/',    include('scripts.urls', namespace='scripts')),
)

clicking 'bash' should retrieve: 点击“ bash”应检索:

site/scripts/templates/scripts/bash.html: 网站/脚本/模板/脚本/bash.html:

<p>we're at bash</p>

Why would a reverse lookup reach the correct URL, but not call the associated view that that URL pattern wants? 为什么反向查询可以到达正确的URL,却不能调用该URL模式所需的关联视图? Thank you 谢谢

The index regex was catching any possible pattern, since it matched any end of string. 索引正则表达式正在捕获任何可能的模式,因为它匹配字符串的任何结尾。 I found out by moving the index pattern after the other 2. It should be: 我通过将索引模式移到其他2之后发现了它。它应该是:

urlpatterns = patterns('',

    url(r'^$',            views.index,           name='index'),
    url(r'^python/$',     views.access_python,   name='python'),
    url(r'^bash/$',       views.access_bash,     name='bash'),
)

Any blank index urls (r'^$') need both start and end of string, to match the empty string after the first part of that pattern (in this case, 'scripts/') 任何空白索引url(r'^ $')都需要字符串的开头和结尾,以匹配该模式第一部分之后的空字符串(在这种情况下为'scripts /')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM