Python Team
Python Team

Reputation: 1171

can't able to get current url in python

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_user(request):
       current_url = request.get_full_path()
   return current_url


class B(A):
    b = A()
    b.get_user()
    print b.current_url


Traceback Error:
    AttributeError: 'A' object has no attribute 'get_full_path'

What mistake i did?

Upvotes: 0

Views: 401

Answers (1)

EMiDU
EMiDU

Reputation: 684

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

Upvotes: 2

Related Questions