How to get a part of the url using aiohttp_jinja2?

Viewed 445

So I am trying to get a part of the URL like http://example/home?code=123456789 this 123456789 changes every time since it is oauth so I am trying to get it

This is the py file


from aiohttp import web,  web_urldispatcher

import discord
from discord.ext import commands


import aiohttp_jinja2

import jinja2
from pathlib import Path


from oauth import Ouath


        @aiohttp_jinja2.template('home.html')
        async def start(request):
            raise web.HTTPSeeOther(location=Ouath.discord_login_url)



        @aiohttp_jinja2.template('home.html')
        async def login(request):
           return
        app = web.Application(loop=self.client.loop)
        aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(str(here)))
        app.router.add_get('/', start)
        app.router.add_get('/home', login)
        runner = web.AppRunner(app)
        await runner.setup()
        self.site = web.TCPSite(runner, '127.0.0.1', 5000)
        await self.client.wait_until_ready()
        await self.site.start()

i want to print it in the html file but i don't know how to get that part note: i edit the code box

4 Answers

Since you're using web from aiohttp you can add a route that accepts a parameter

routes = web.RouteTableDef()
@routes.get('/guild/{guild}')
async def guild(request):
    gid = request.match_info['guild']

The url would be http://localhost:PORT/guild/123456 Once you've fetched the required details, you're free to render a template or return a response.

If you have string then you can use

URL = 'http://example/home?code=123456789'

code = URL.split('?code=')[-1]

or if number is always at the end and it has always the same lenght

URL = 'http://example/home?code=123456789'

code = URL[-9:] 

But there is also urllib.parse and

URL = 'http://example/home?code=123456789'

data = urllib.parse.parse_qs(urllib.parse.urlsplit(URL).query) 

gives dictionary

{'code': ['123456789']}

and you can do

code = data.get('code')

and it will gives expected code or None if there was no ?code=... in url.


EDIT Probably you have to use request.url

@aiohttp_jinja2.template('home.html') 
async def login(request): 
     data = urllib.parse.parse_qs(urllib.parse.urlsplit(request.url).query)
     code = data.get('code')
     return {'code': code}

Because data is dictionary with "code" so you could use return data

@aiohttp_jinja2.template('home.html') 
async def login(request): 
     data = urllib.parse.parse_qs(urllib.parse.urlsplit(request.url).query)
     return data

After digging in source code of aiohttp_jinja2 and aiohttp it seems you can get it with request.query.get('code')

@aiohttp_jinja2.template('home.html') 
async def login(request): 
    #print('code:', request.query.get('code'))
    return {'code': request.query.get('code')} 

If there is not ?code=... in URL then it gives None but you can set other default value using request.query.get('code', some_default_value)


Doc aiohttp: web.BaseRequest.query

@aiohttp_jinja2.template('home.html') 
async def login(request): 
code = urllib.parse.parse_qs(urllib.parse.urlsplit(request).query)
return {'code': code} 

like this ?

Related