开发者

Pass a variable ( a flag ) from urls.py to views.py in django

开发者 https://www.devze.com 2023-04-13 07:18 出处:网络
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.

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>[^/]+)/$', 'entries',  name='blog'),
              url(r'^(?P<blog_author>[^/]+)/(?P<entry_slug>[^/]+)/$', '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/alumn开发者_开发知识库i/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.


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'),
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号