如何将用户实例传递给 Django 中的视图
我正在制作一个博客应用程序,我想在其中创建一个显示特定用户的所有博客的视图。为此,我需要将用户实例传递到我的视图,
def blogs(request,author=None,slug=None):
# If the author context has been passed then display the blogs of that author
if author:
# Displays a particular blog
if slug:
this_blog = get_object_or_404(Entry, creator = author, slug = slug)
return render_to_response('blog/blog_view.html', {'blog': this_blog,},context_instance=RequestContext(request))
# Displays all the blogs of a particular user
else:
blog_list = Entry.objects.filter(creator = author)
return render_to_response('blog/all_blogs_user.html', {'Blogs': blog_list},context_instance=RequestContext(request))
尽管从语法上讲这是正确的,但现在我不知道如何在我的网址中实际传递此用户上下文。早些时候,我尝试仅传递用户 ID,但这不起作用。除了做这件事还有其他选择吗?当我在内部构建 url 或重定向到此特定视图时,这很好,但是 url 在外部看起来如何。我的 urls.py 是
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
url(r'^$', 'blog.views.blogs', name='all_blogs'),
url(r'^(?P<author>\d+)/(?P<slug>[-\w]+)/$', 'blog.views.blogs', name='view_blog'),
url(r'^(?P<author>\d+)/(?P<slug>[-\w]+)/edit/$', 'blog.views.post_form', name='edit_blog'),
url(r'^new/$', 'blog.views.post_form', name='new_blog'),
url(r'^(?P<author>\d+)/(?P<slug>[-\w]+)/delete/$', 'blog.views.delete_blog', name='delete_blog'),
)
urlpatterns += staticfiles_urlpatterns()
I am making a blog app where I want to make a view that displays all the blogs of a particular user. For this I need to pass the user instance to my view as
def blogs(request,author=None,slug=None):
# If the author context has been passed then display the blogs of that author
if author:
# Displays a particular blog
if slug:
this_blog = get_object_or_404(Entry, creator = author, slug = slug)
return render_to_response('blog/blog_view.html', {'blog': this_blog,},context_instance=RequestContext(request))
# Displays all the blogs of a particular user
else:
blog_list = Entry.objects.filter(creator = author)
return render_to_response('blog/all_blogs_user.html', {'Blogs': blog_list},context_instance=RequestContext(request))
Although syntactically this is correct but now I do not how to actually pass this user context in my url. Earlier I tried passing just the user id but that does not work. Is there any other alternative to doing this thing. When I am building the url internally or redirecting to this particular view then it is fine, but how would the url look like externally. My urls.py is as
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
url(r'^
, 'blog.views.blogs', name='all_blogs'),
url(r'^(?P<author>\d+)/(?P<slug>[-\w]+)/
, 'blog.views.blogs', name='view_blog'),
url(r'^(?P<author>\d+)/(?P<slug>[-\w]+)/edit/
, 'blog.views.post_form', name='edit_blog'),
url(r'^new/
, 'blog.views.post_form', name='new_blog'),
url(r'^(?P<author>\d+)/(?P<slug>[-\w]+)/delete/
, 'blog.views.delete_blog', name='delete_blog'),
)
urlpatterns += staticfiles_urlpatterns()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它通常的工作方式如下:
urls.py
views.py
It generally works like this:
urls.py
views.py
您只需通过 request.user 即可访问视图中的用户。同样,您也可以在请求对象中POST数据。
You can simply access the user in the view by request.user. Similarly, you can POST data in the request object as well.