can't able to get current url in python

前端 未结 1 968
借酒劲吻你
借酒劲吻你 2021-01-26 05:08

I have created a class and method as like below. I need to get current page url. But am getting error while calling get_full_path().

class A(object):
   def get_         


        
1条回答
  •  野的像风
    2021-01-26 05:56

    This code cannot work because has some misconceptions:

    1. You should pass request to get_user
    2. First argument of get_user should be self which points to A instance.
    3. Object cannot return value (you have return current_url in A class).
    4. It is not necessary to use class inheritance in this example.

    Your code should look like:

    class A(object):
        current_url = None
        def get_user(self, request):
            self.current_url = request.get_full_path()
    
    b = A()
    b.get_user(request)
    print b.current_url
    

    Or if you want to pass full path to constructor of class A, and for whatever reason use it in inherited class B:

    class A(object):
        def __init__(self, current_url):
            self.url = current_url
    
        def get_path(self):
            return self.current_url
    
    class B(A):
        # whatever you need here
        pass
    
    b = B(request.get_full_path())
    print b.get_path() # prints current path
    

    0 讨论(0)
提交回复
热议问题