在django中将变量(标志)从urls.py传递到views.py

发布于 2024-12-10 11:17:16 字数 887 浏览 0 评论 0原文

我目前有一个“博客”应用程序,它将根据用户名显示不同用户的博客文章。这就是我的 urls.py 的样子。

urlpatterns = patterns('blog.views',
              url(r'^(?P<blog_author>[^/]+)/$', 'entries',  name='blog'),
              url(r'^(?P<blog_author>[^/]+)/(?P<entry_slug>[^/]+)/$', 'blog_entry', name='blog_entry'),
          )

因此,blog/authorname/ 将列出该特定作者的所有博客文章条目,blog/authorname/foo-post/ 将呈现特定的博客文章。我想做的是将校友成员的博客文章分别永久重定向到 blog/alumni/authornameblog/alumni/authorname/foo-post/ 。我在我的用户模型中添加了一个字段,该字段将指示用户是否是校友。

普通用户和校友的浏览功能基本相同。这是我到目前为止一直在尝试做的事情:在 entries 视图函数中,我添加了一些行来检查校友成员。如果某个博客作者是校友,则会HttpResponsePermanentRedirectblogs/alumni/alumni_author/,并且由于渲染部分相同,因此调用相同的entries代码>查看函数。所以基本上,我的 entries 函数正在尝试检查会员类型为普通会员和校友会员呈现视图

I currently have a 'blog' app which will display blogposts by different users depending on the username. This is what my urls.py looks like.

urlpatterns = patterns('blog.views',
              url(r'^(?P<blog_author>[^/]+)/

So, blog/authorname/ would list out all the blogpost entries by that particular author and blog/authorname/foo-post/ will render a particular blog post. What I am trying to do is to permanently redirect blogposts of alumni members to blog/alumni/authorname and blog/alumni/authorname/foo-post/ respectively. I have added a field in my user model which will indicate whether a user is an alumni or not.

The views function is basically the same for both normal users and alumni. Here is what I have been trying to do so far: Inside the entries view function, I added some lines that would do the checking of alumni member. If a certain blog author is an alumni, will HttpResponsePermanentRedirect to blogs/alumni/alumni_author/ and since the rendering part is the same, call the same entries view function. So basically, my entries function is trying to do checking member types, rendering view for both normal and alumni members.

, 'entries', name='blog'), url(r'^(?P<blog_author>[^/]+)/(?P<entry_slug>[^/]+)/

So, blog/authorname/ would list out all the blogpost entries by that particular author and blog/authorname/foo-post/ will render a particular blog post. What I am trying to do is to permanently redirect blogposts of alumni members to blog/alumni/authorname and blog/alumni/authorname/foo-post/ respectively. I have added a field in my user model which will indicate whether a user is an alumni or not.

The views function is basically the same for both normal users and alumni. Here is what I have been trying to do so far: Inside the entries view function, I added some lines that would do the checking of alumni member. If a certain blog author is an alumni, will HttpResponsePermanentRedirect to blogs/alumni/alumni_author/ and since the rendering part is the same, call the same entries view function. So basically, my entries function is trying to do checking member types, rendering view for both normal and alumni members.

, 'blog_entry', name='blog_entry'), )

So, blog/authorname/ would list out all the blogpost entries by that particular author and blog/authorname/foo-post/ will render a particular blog post. What I am trying to do is to permanently redirect blogposts of alumni members to blog/alumni/authorname and blog/alumni/authorname/foo-post/ respectively. I have added a field in my user model which will indicate whether a user is an alumni or not.

The views function is basically the same for both normal users and alumni. Here is what I have been trying to do so far: Inside the entries view function, I added some lines that would do the checking of alumni member. If a certain blog author is an alumni, will HttpResponsePermanentRedirect to blogs/alumni/alumni_author/ and since the rendering part is the same, call the same entries view function. So basically, my entries function is trying to do checking member types, rendering view for both normal and alumni members.

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

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

发布评论

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

评论(1

难得心□动 2024-12-17 11:17:16

您可以使用可选的第三个参数将标志从 urlconf 发送到视图:

url(r'^alumni/(?P<blog_author>[^/]+)/
, 'entries', {'alumni': True}, name='blog'),

You can send a flag from the urlconf to the view by using the optional third parameter:

url(r'^alumni/(?P<blog_author>[^/]+)/
, 'entries', {'alumni': True}, name='blog'),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文