How can I get the reverse url for a Django Flatpages template
如何获得Django Flatpages模板的反向url ?
22
Include flatpages in your root urlconf:
在你的根urlconf中包含平装页:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
('^pages/', include('django.contrib.flatpages.urls')),
)
Then, in your view you can call reverse like so:
那么,在您看来,您可以这样调用reverse:
from django.core.urlresolvers import reverse
reverse('django.contrib.flatpages.views.flatpage', kwargs={'url': '/about-us/'})
# Gives: /pages/about-us/
In templates, use the {% url %} tag (which calls reverse internally):
在模板中,使用{% url %}标签(在内部调用反向):
<a href='{% url django.contrib.flatpages.views.flatpage url="/about-us/" %}'>About Us</a>
24
I prefer the following solution (require Django >= 1.0).
我更喜欢下面的解决方案(需要Django >= 1.0)。
settings.py
INSTALLED_APPS+= ('django.contrib.flatpages',)
urls.py
urlpatterns+= patterns('django.contrib.flatpages.views',
url(r'^about-us/$', 'flatpage', {'url': '/about-us/'}, name='about'),
url(r'^license/$', 'flatpage', {'url': '/license/'}, name='license'),
)
[...]
<a href="{% url about %}"><span>{% trans "About us" %}</span></a>
<a href="{% url license %}"><span>{% trans "Licensing" %}</span></a>
[...]
from django.core.urlresolvers import reverse
[...]
reverse('license')
[...]
That way you don't need to use django.contrib.flatpages.middleware.FlatpageFallbackMiddleware
and the reverse works as usual without writing so much code as in the other solutions.
这样您就不需要使用django.悔过书。flatpage .middleware了。FlatpageFallbackMiddleware和其他解决方案一样,不需要像在其他解决方案中那样编写那么多的代码。
Cheers.
欢呼。
3
I agree with Anentropic that there is no point in using Django Flatpages if you need to write urlconfs to employ them. It's much more straightforward to use generic views such as TemplateView
directly:
我同意Anentropic的观点,如果你需要写urlconfs来使用Django平面页面是没有意义的。直接使用TemplateView这样的通用视图要简单得多:
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^about/$', TemplateView.as_view(template_name="about.html"), name="about"),
)
Flatpages take advantage of FlatpageFallbackMiddleware
, which catches 404 errors and tries to find content for requested url in your database. The major advantage is that you don't have to touch your templates directly each time you have to change something in them, the downside is the need to use a database :)
Flatpages利用了FlatpageFallbackMiddleware,它捕获了404错误并试图在数据库中为请求的url查找内容。最大的好处是,你不必每次都要修改模板就直接接触你的模板,缺点是需要使用数据库:)
If you still choose to use Flatpages app, you'd better use get_flatpages
template tag:
如果你仍然选择使用Flatpages app,你最好使用get_flatpages模板标签:
{% load flatpages %}
<ul>
{% for page in get_flatpages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
Personally, I rarely reference flatpages outside of website's main menu, which is included via {% include 'includes/nav.html' %}
and looks like this:
就我个人而言,我很少在网站的主菜单之外引用flatpages,这包括通过{% include /nav。html' %},如下所示:
<ul>
<li><a href="/about/">About</a></li>
<li><a href="/credits/">Credits</a></li>
...
</ul>
I don't feel I violate any DRY KISS or something:)
我觉得我没有违反任何干吻之类的东西。
2
Write your base urls conf to point to your flatpages. Assume it is under pages:
写你的基本url conf指向你的页。假设它在页下面:
urlpatterns = patterns('',
...
url(r'^pages/', include('project.pages.urls')),
...
)
Then write your flatpages as normal:
然后把你的平板电脑写得很正常:
urlpatterns = patterns('django.views.generic.simple',
url(regex=r'^resume/$', view='direct_to_template', kwargs={'template': 'resume.html'}, name='resume'),
url(regex=r'^about/$', view='direct_to_template', kwargs={'template': 'about.html'}, name='about'),
url(regex=r'^books/$', view='direct_to_template', kwargs={'template': 'library.html'},name='books'),
)
Then your template just refers to them in the usual fashion:
那么你的模板只是以通常的方式引用它们:
<div id="pages">
...
<div class="pagelinks">
<a href="{% url about %}">ABOUT</a>
</div>
</div>
2
I thought the advantage of Flatpages was you didn't have to create any view stubs or url confs? It's a bit pointless otherwise... if you're creating views and urls you may as well save the flatpage content as template html instead.
我认为平面页面的优点是你不需要创建任何视图存根或url conf ?这是毫无意义的,否则……如果您正在创建视图和url,您也可以将页面内容保存为模板html。
try this instead: http://wiki.github.com/0sn/nameremoved/flatpages
试试这个:http://wiki.github.com/0sn/nameremoved/flatpages
1
When you create any flatpage, you need to specify an URL which is saved as part of the model. Hence you can retrieve the URL from any flatpage object. In a template:
创建任何flatpage时,都需要指定作为模型一部分保存的URL。因此,您可以从任何flatpage对象中检索URL。在一个模板:
{{ flatpage.url }}
Remapping flatpage URLs in urls.py
and then having to use reverse sort of defeats the purpose of the flatpages app.
在url中重新映射flatpage url。然后使用反向操作就会破坏flatpages程序的目的。
1
None of the solutions mentioned sufficiently followed the DRY principle in my opinion, so I just did this:
在我看来,没有一个解决方案充分遵循了DRY原则,所以我就这样做了:
# core/templatetags/hacks.py
from django import template
register = template.Library()
@register.simple_tag
def flaturl(title):
"""
Returns the url for a flatpage based on the title.
NOTE: Obviously the title must be unique.
"""
from django.contrib.flatpages.models import FlatPage
try:
page = FlatPage.objects.get(title=title)
except:
return ""
return page.url
Then in any template that needs to make a link, I did this:
然后在任何需要建立链接的模板中,我这样做了:
{% load hacks %}
...
<a href="{% flaturl 'My Page Title' %}">Page Title</a>
I might add some caching in there to keep the performance up, but this works for me.
我可能会在其中添加一些缓存以保持性能,但这对我来说是可行的。
1
You need to redeclare the url conf and cannot rely on the official 'django.contrib.flatpages.urls'
that the doc is encouraging us to use.
您需要重新声明url conf,不能依赖于官方的“django.悔过书”。文档鼓励我们使用的url。
This won't be more difficult, just include in your urls.py
这不会更困难,只包括在你的小瓶子里
from django.conf.urls import patterns, url
urlpatterns += patterns('',
...
url(r'^pages(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage', name='flatpage'),
...
)
And now you can use your usual reverse url template tag
现在您可以使用通常的反向url模板标记
<a href='{% url 'flatpage' url="/about-us/" %}'>About Us</a>
Or to display a list of all flat pages
或显示所有平面页面的列表
<ul>
{% get_flatpages as flatpages %}
{% for page in flatpages %}
<li><a href="{% url 'flatpage' url=page.url %}">{{ page.title }}</a></li>
{% endfor %}
</ul>
1
proper Django>= 1.10:
适当的Django > = 1.10:
urls.py
urls . py
urlpatterns += [
url(r'^(?P<url>.*/)$', flatpage, name='flatpage'),
]
easy lookup inside the template:
模板内部易于查找:
{% url "flatpage" url="SOME_URL" %}
where SOME_URL is the value frome the flatpage.url
field
其中SOME_URL是与页面平齐的值。url字段
0
According to this django documentation for flatpages
根据这个django平页文档
You can simply do
你可以简单的做
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
In your template.
在你的模板。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/07/17/c07be3215fb5b7a56dd611d5bf787dc0.html。