简体   繁体   中英

passing GET parameter from python view to template

As simple as this is, I'm having trouble getting the value of a GET parameter d

I'm clicking on a link like:

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

The view that, that url serves is:

@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                                                     
    })

specifically, this line:

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

the value of DomainToScan is outputting as None when viewing the django template begin_site.html

What am I doing wrong here?

Thank you!

UPDATE:

urls.py

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

For some reason this isn't matching a url like:

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

Any reason why? Rubular says its valid

You don't have any GET parameters. If you want domain to be a GET parameter, your link should be http://127.0.0.1:8000/Account/Site/?d=mysite.com - notice the ? .

(Also note you would need to actually return HttpResponseRedirect() , not just call it: but the whole checking of is_authenticated is pointless anyway because the function is decorated with login_required, so a non-authenticated user wouldn't even get into that view.)

Try:

@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),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM