将GET参数从python视图传递给模板

时间:2014-05-01 12:47:56

标签: python django view parameters get

这很简单,我无法获取GET参数的值d

我点击了以下链接:

http://127.0.0.1:8000/Account/Site/d=mysite.com

view所服务的url是:

@login_required
def Site(request):
    if request.user.is_authenticated():
        # verify domain in url is associated with authenticated users account
        DomainToScan = request.GET.get('d')
        VerifyDomainAgainstDb = Tld.objects.filter(FKtoClient=request.user,domainNm=DomainToScan)
    else:
        HttpResponseRedirect("/Login/")

    return render(request, 'VA/account/begin_site.html', {
        'Site':Site,
        'VerifyDomainAgainstDb':VerifyDomainAgainstDb                                                     
    })

具体来说,这一行:

DomainToScan = request.GET.get('d')

在查看django模板DomainToScan时,None的值输出为begin_site.html

我在这里做错了什么?

谢谢!

更新

urls.py

(r'^Account/Site/[?]d=[A-za-z0-9]{2,50}.[a-z]{1,3}$', Site),

出于某种原因,这不符合以下网址:

http://127.0.0.1:8000/Account/Site/?d=mysite.com

有什么理由? Rubular表示有效

2 个答案:

答案 0 :(得分:1)

您没有任何GET参数。如果您希望domain成为GET参数,则您的链接应为http://127.0.0.1:8000/Account/Site/?d=mysite.com - 请注意?

(另请注意,您需要实际返回 HttpResponseRedirect(),而不仅仅是调用它:但是is_authenticated的整个检查无论如何都是没有意义的,因为函数是用login_required修饰的,所以非 - 经过验证的用户甚至不会进入该视图。)

答案 1 :(得分:-1)

尝试:

@login_required
def Site(request, DomainToScan=None):
    if request.user.is_authenticated():
        # verify domain in url is associated with authenticated users account
        VerifyDomainAgainstDb = Tld.objects.filter(FKtoClient=request.user,domainNm=DomainToScan)
    else:
        return HttpResponseRedirect("/Login/")
    return render(request, 'VA/account/begin_site.html', locals())

urls.py

(r'^Account/Site/(?P<DomainToScan>[^/]+)/', Site),