如何将用户实例传递给 Django 中的视图

发布于 2024-12-20 10:08:55 字数 1645 浏览 2 评论 0原文

我正在制作一个博客应用程序,我想在其中创建一个显示特定用户的所有博客的视图。为此,我需要将用户实例传递到我的视图,

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 技术交流群。

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

发布评论

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

评论(2

自演自醉 2024-12-27 10:08:55

它通常的工作方式如下:

urls.py

url(r'/author/(?P<slug>/

views.py

def author_index(request, slug):
    author = get_object_or_404(User, username=slug)

    return render_to_response('author_index.html', {
        'author': author,
    }, context_instance=RequestContext(request))
, 'author_index'),

views.py

It generally works like this:

urls.py

url(r'/author/(?P<slug>/

views.py

def author_index(request, slug):
    author = get_object_or_404(User, username=slug)

    return render_to_response('author_index.html', {
        'author': author,
    }, context_instance=RequestContext(request))
, 'author_index'),

views.py

月下伊人醉 2024-12-27 10:08:55

您只需通过 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.

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