Name of the user not getting displayed in python code - python

I have made a signup page using python. Following is the link to the code :
Signup Page Code
As soon as a user signs up, he should be redirected to /welcome which says :
Welcome user
But I cannot display it by the above mentioned code.
Whereas when I write
self.redirect('/welcome?username = '+user)
I get redirected to a page which says
Welcome user
Why is it so?

When you write:
self.redirect('/welcome?username=' + user) #no whitespaces in the url
you are adding to the url a GET parameter(of the type name=value), in your case username=user.
So, when your welcomeHandler class handles the get request:
username=self.request.get('username')
it sets the username to the value of the GET parameter in the url with the name 'username' (username = user) and your welcome message is correctly displayed.
If you:
self.redirect('/welcome')
there are no GET parameters in the url and username is set to the default empty string and just Welcome is displayed.
Check here the Request documentation.

Related

What actually get_Object() takes as a parameter in Fabook SDK?

I want to send friend name as a user in get_object()in below code for getting his public posts. But I am getting error
raise GraphAPIError(result)
facebook.GraphAPIError: (#803) Cannot query users by their username (tayyab.rasheed.545)
user = 'tayyab.rasheed.545' #is giving error
#user = 'BillGates' #is working fine.
# user = 'me' #is working fine.
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')
Why is the error? I think I am doing something wrong. BillGates and me is working fine then why not tayyab.rasheed.545 Profile of friend is 'https://www.facebook.com/tayyab.rasheed.545'
Why is the error?
Because Facebook removed the username field from the API with v2.0, and as the error message says, you can not query user profiles by their username any more.
BillGates and me is working fine then why not `tayyab.rasheed.545
BillGates simply is a Facebook Page, and not a user profile.
(And me has nothing to do with the username in the first place.)
It is not possible to get public posts of any user. Even for you own public posts, you need to authorized yourself with the user_posts permission.
Edit: Please donĀ“t change your question, especially when there is an answer already. CBroes answer is correct. You are not supposed to get any data of users who did not authorized your App anyway.

Testing url redirection in Django

If I go to http://localhost:8000/login/, login form will be displayed and you have to input username and password. If the credentials are right then you will be redirected to http://localhost:8000/dashboard/. The problem I am facing while testing is even though my username and password are correct it is not redirected to http://localhost:8000/dashboard/ but rather it goes to http://testserver/admin/login/?next=/dashboard/. I am using the below code to rest redirection functionality in django:
class UrlTests(TestCase):
def test_client_cart(self):
response = self.client.get('/login/')
self.assertEqual(200, response.status_code)
def test_login_client(self):
User.objects.create_user('rakesh', 'rrs402#nyu.edu', 'ranjan')
self.client.get('/login/')
self.client.login(username='rakesh', password='ranjan')
print self.client.get('/dashboard/')
Could anyone tell me why i am redirected to http://testserver/admin/login/?next=/dashboard/ instead http://localhost:8000/dashboard/.
In my settings.py file:
LOGIN_URL = '/'
LOGOUT_URL = '/logout/'
LOGIN_REDIRECT_URL = '/dashboard/'
LOGOUT_REDIRECT_URL = '/'
My application is working fine but I am not able to test redirection thing. Could anyone give me some pointers where I am going wrong?
If I print out print self.client.login(username='rakesh', password='ranjan') it is coming out to be True which means I am able to login but why not redirection ?
Update : I also tried using selenium
def login_user(self):
# User opens his web browser, and goes to the website
self.browser.get(self.live_server_url + '/login/')
# User types in his username and passwords and hits return
username_field = self.browser.find_element_by_name('username')
username_field.send_keys('rakesh')
password_field = self.browser.find_element_by_name('password')
password_field.send_keys('ranjan')
#User submits the form
password_field.send_keys(Keys.RETURN)
body = self.browser.find_element_by_tag_name('body')
print body.text
Here also I am getting the body.text of admin page but not dashboard page.
Update :
Dashboard view :
class PortalDashboardView(RequireStaffMixinView, TemplateView):
template_name = "portal/dashboard.html"
Because you aren't getting logged in. The password you are sending to the login call is different to the one you use when creating the user within your test case, so the credentials are invalid.
Note you don't need to call client.get('/login/') there, anyway.

How to pass URL arguments after # sign in Django

If I send a link to a Django page which has #args in the URL, e.g.
http://localhost/someurl/?arg1=true&arg2=false#1970-01-01/2038-01-01/something_else
Then the person is not logged in - the login form appears. Unfortunately after successful login, it will redirect the person to:
http://localhost/someurl/?arg1=true&arg2=false
What I would like to keep somehow is also the #1970-01-01/2038-01-01/something_else
How can I do that in Django?
I would try modifying the code responsible for generating the link such that the # symbol is replaced by %23 (i.e. its percent encoding: https://en.wikipedia.org/wiki/Percent-encoding ).
The final URL would be:
http://localhost/someurl/?arg1=true&arg2=false%231970-01-01/2038-01-01/something_else

css working on all but two pages

Specifically, pages using an optional regular expression. By optional, I mean PAGE_RE below.
I am creating a Wiki. If a user searches a term, and that term doesn't already exist, then the user is redirected to an edit page so they can create the new content. This only happens, however, if the user is logged in. To determine if the user is logged in, I check for a cookie. If the user isn't logged in(no cookie), then I redirect, not to the edit page, to create the new content, but to the login page, dealt with by the Login class below. The user logs in, a cookie is created, and then they are redirected to the edit page to create the content they originally searched for. In order to remember what their topic was, so I can redirect after the login, I send the topic(in the form '/topic') to the Login class, where it's received as a parameter by the get and post methods. If a user just comes to the site and logs in normally, they are redirected to the home page, but in this case, because the topic has been received by get and post and is not None, I use the line self.redirect('/edit/%s' % topic[1:]) below, to send them on to their original destination. The problem is, css isn't working for the two urls in below that use PAGE_RE. JsFiddler4 shows that there is a 404 involving /login/css/wiki.css. It suddenly clicked after some time that that url is not the url for the login page when it receives the extra 'topic' param. It is also the case with EditPage. How can I get css to work on these pages/urls when they are sometimes different? I didn't know what was going on for ages, then I downloaded and ran JsFiddler4 and figured it out. I am using Google App Engine, webapp2, jinja2. Any help much appreciated. Apart from those two pages, css works fine.
This code is out of order and incomplete, but I hope it's sufficient
PAGE_RE = r'(/?(?:[A-Za-z0-9_-]+/?)*)'
app = webapp2.WSGIApplication([
('/signup', Register),
('/logout', Logout),
('/login' + PAGE_RE, Login),
('/edit' + PAGE_RE, EditPage),
('/', Front),
(PAGE_RE, WikiPage),
], debug=True)
class Login(Main):
""" Validate form and validate users cookie """
def get(self, topic):
self.render('login.html', error={})
def post(self, topic):
username = self.request.get('username')
password = self.request.get('password')
if not username or not self.valid(user=username):
self.login_error(user=username)
elif not password or not self.valid(pw=password):
self.login_error(user=username)
elif not self.user_exists(username):
self.render('login.html', error={'no_user':'That user does not exist'})
else:
self.login(username, password, topic=topic)
def login(self, name, pw, topic):
user_hash = User.get_user_hash(name)
if self.valid_pw(name, pw, user_hash):
self.create_secure_cookie('user_id', name)
if topic:
self.redirect('/edit/%s' % topic[1:])
else:
self.redirect('/')
else:
self.login_error(user=name)
def login_error(self, user):
self.render('login.html', username=user, error={'login': errors['login']})
def valid_pw(self, name, pw, user_hash):
salt = user_hash.split('|')[0]
return user_hash == self.create_user_hash(name, pw, salt)
Ok, I solved this. Here's what appeared to be happening.
This redirect was sending a parameter to the Login class above:
self.redirect('login/%s' % wiki_topic) #wiki_topic = something like 'topic'
when looking for the css for a page, what seems to happen is that the last part of the path up to the '/' is taken off, and replaced by the path to the css, '/css/wiki.css' in my case.
So I was passing 'login/topic' and just 'topic' was being replaced by the css path to give:
'login/css/wiki.css' instead of the correct just 'css/wiki/css'.
To stop this from happening, I changed the line redirecting to the Login class from:
self.redirect('login/%s' % wiki_topic) to >> self.redirect('login%s' % wiki_topic)
the second version has no slash before the %s.

How do I redirect an user back to the page they were trying to access once they log in? (Django)

So currently I'm using #login_required to block certain pages from users and redirect them, telling them they need to log in. but what I can't understand is how do I "let them" go to the page they were trying to go to once they log in. Currently I'm just using a typical render_to_response('with a certain view') but what if i want that response to be anywhere where they were trying to access. How do i code that?
The #login_required will generally pass you back the redirect_field_name (default is "next") for example: /accounts/login/?next=/polls/3/. So in your login view after authenticating and logging in the user you can do something like
response = HttpResponseRedirect(next)
# Do whatever else you need to do here with the response object
return response
See the docs at https://docs.djangoproject.com/en/1.3/topics/auth/#the-login-required-decorator
You can pass a url parameter back to your login page and use that to direct the user once they complete the login successfully.
from the login requiered decorator docs it says:
By default, the path that the user should be redirected to upon
successful authentication is stored in a query string parameter called
"next".
and usually when the login is done it take to the "next" url
Here's what django.contrib.auth.views.login does:
If called via GET, it displays a login form that POSTs to the same
URL. More on this in a bit.
If called via POST, it tries to log the
user in. If login is successful, the view redirects to the URL
specified in next. If next isn't provided, it redirects to
settings.LOGIN_REDIRECT_URL (which defaults to /accounts/profile/). If
login isn't successful, it redisplays the login form.

Categories