super9
super9

Reputation: 30101

Question about getting urls in Django

I have this page http://127.0.0.1:8000/user/tom-brandy/.

There is a link on the page to http://127.0.0.1:8000/signup/.

How can I extract tom-brandy in my view that renders the signup page?

I was thinking of using request.META['HTTP_REFERER'] and using string manipulation to get it but it sounds like a wrong way to go about this.

Is there a better way to do this?

Upvotes: 0

Views: 102

Answers (3)

You could explicitly pass the user information to the signup link as a get parameter /signup/?user=tom-brandy

You could also use the session to set an arbitrary variable in the user pages and pull it in the signup page. That would probably be the cleanest and most transparent method.

('user/(?P<user>[\w-]+)/', 'my.user_view')

def user_view(request, user):
    request.session['last_visited_user_page'] = user
    # ...

def signup_view(request):
    last_visited_user_page = request.session.get('last_visited_user_page')
    # ...

Upvotes: 5

Lockjaw
Lockjaw

Reputation: 1902

You can use HttpRequest.path_info or HttpRequest.path as required.

The relevant documentation: HttpRequest.path_info

Upvotes: 0

Guilherme David da Costa
Guilherme David da Costa

Reputation: 2368

You can use a regular expression on your urls that extracts tom-brandy out of http://127.0.0.1:8000/signup/tom-brandy and passes that as a first argument to your signup view

Upvotes: 0

Related Questions