我如何在Django重定向中使用Kwargs,我遇到一个错误

时间:2019-07-01 18:44:24

标签: python django python-3.x

我想为我的详细信息页面创建一个带有kwargs的重定向For x = 14 To 434 If CDate(Cells(x,16).Value) > CDate(Cells(x,15).Value) Then With Cells(x,16).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent6 End With End If Next x 。 我有一个评论系统,我想重定向到用户所在的页面。

我的网址:

Django

我的详细信息:

from django.urls import path, re_path
from .views import blog, detail, posts_by_tag

app_name = "blog"
urlpatterns = [
    re_path('^$',blog, name="blog"),
    re_path('^(?P<year>[\d+]{1,4})/(?P<month>[\d+]{1,2})/(?P<day>[\d+] 
               {1,2})/(?P<slug>[\w-]+)/$', detail, name= "detail"),
    re_path('^(?P<tag>[\w-]+)/$',posts_by_tag, name="tag"),
]

我的错误是:

def detail(request, slug, year, month, day):
    post = get_object_or_404(Post,slug=slug,
                                created__year = year,
                                created__month = month,
                                created__day = day)
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == "POST":
        comment_form = CommentForm(request.POST or None)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post

            new_comment.save()
            return redirect('blog:detail', kwargs= 
{'slug':slug,'created__year':year,'created__month' : month, 
'created__day' : day,})
    else:
        comment_form = CommentForm()
    context = {
        'post':post,
        'comments':comments,
        'new_comment':new_comment,
        'comment_form':comment_form,
    }
    return render(request, 'blog/detail.html', context)

1 个答案:

答案 0 :(得分:0)

文档在examples section中显示了这一点:

  

2)通过传递视图名称以及可选的一些位置或   关键字参数;该网址将使用   .main{ height: 500px; width: 500px; background: blue; } .hidden{ height: 0px; width: 300px; background-color: red; transform-origin: 100% 0; transition: all 1.0s; position: relative; top: 200px; } .hidden:hover{ height:200px; top: 0px; transition: all 1.0s; } 方法:

reverse()

您尝试过吗?

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')