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开发者_Python百科.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?
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')
# ...
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
You can use HttpRequest.path_info
or HttpRequest.path
as required.
The relevant documentation: HttpRequest.path_info
精彩评论