为什么 django page cms get_absolute_url 返回空字符串?

发布于 2024-09-29 08:34:15 字数 1059 浏览 12 评论 0原文

使用 django page cms 时,我的 django 模板中的 get_absolute_url 遇到问题。 它返回一个空字符串,并且不链接到我所需的页面。

我有以下模型、URL 模板和视图

模型

class Body(models.Model):
...
url = models.SlugField(unique=True, help_text='---')

url

(r'^news/', include('news.urls_news')),......

url(r'^/(?P<url>[\w\-]+)/$', 'news_view', name='news_view'),    

视图

def news_view(request, url):
new = get_object_or_404(Body, url=url)
return render_to_response('news/view.html', {
    'news': news
}, context_instance=RequestContext(request))

模板

   <li><a href="{{ news.get_absolute_url }}">{{ news.title }}</a></li>

我的模板中的以下代码返回我想要的字符串,但这确实会定向到我的 html 页面

   <li><a href="{{ news.url }}">{{ news.title }}</a></li>

我知道我的所有内容都链接到正确的文件,因为我还有其他有效的视图正确。有人可以指出正确的方向,为什么 get_absolute_url 无法正常工作以及为什么 {{ news.url }} 没有定向到正确的页面。我确信它与我的 urls.py 有关,但我不确定。 请耐心等待,我是 django 的新手。非常感谢所有帮助。

I am having issues with get_absolute_url in my django templates when using django page cms.
It returns an empty string and does not link to my required page.

I have the following Models, URLs templates and views

Models

class Body(models.Model):
...
url = models.SlugField(unique=True, help_text='---')

urls

(r'^news/', include('news.urls_news')),......

url(r'^/(?P<url>[\w\-]+)/

View

def news_view(request, url):
new = get_object_or_404(Body, url=url)
return render_to_response('news/view.html', {
    'news': news
}, context_instance=RequestContext(request))

Template

   <li><a href="{{ news.get_absolute_url }}">{{ news.title }}</a></li>

the following code in my template returns the string I desire however this does to direct to my html page

   <li><a href="{{ news.url }}">{{ news.title }}</a></li>

I know my everything links to the correct files because I have other views that work correctly. Could somebody please point me in the correct direction as to why get_absolute_url is not working correctly and why {{ news.url }} does not direct to the correct page. I am sure it has something to do with my urls.py however I am not certain.
Please bear with me I am new to django. All help is greatly appreciated.

, 'news_view', name='news_view'),

View

Template

the following code in my template returns the string I desire however this does to direct to my html page

I know my everything links to the correct files because I have other views that work correctly. Could somebody please point me in the correct direction as to why get_absolute_url is not working correctly and why {{ news.url }} does not direct to the correct page. I am sure it has something to do with my urls.py however I am not certain.
Please bear with me I am new to django. All help is greatly appreciated.

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

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

发布评论

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

评论(1

等待我真够勒 2024-10-06 08:34:15

您是否确实在 News 模型上定义了 get_absolute_url 方法?你不表现出来。

Have you actually defined a get_absolute_url method on the News model? You don't show it.

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