how to pass id in url after click on signin button in django and go to user dashboard page - django-views

how to pass id in url after click on signin button in django, dashboard change accorrding signin id ,which id pass in url. I am trying to pass id in url,when user click on signin button.if another user signin then pass that user id in url.

Related

How to use Flask 'next' for pages without decorator '#login_required' [duplicate]

This question already has answers here:
How to pass a variable between Flask pages?
(2 answers)
Closed last year.
I am creating a web application in which there is a page (let's call it 'event' page) which is not login protected(i.e. users can see the page without logging in).
The page shows some event details to which user can select if they are attending or not by clicking appropriate button.
Upon clicking the button, user is redirected to login page since they need to login before they can choose an option.
Once the user is goes to login page, I want to redirect the user back to the 'event' page after they login. How do I do that?
If the 'event' page was login protected, I know I can use Flask 'next' to redirect user to the page they were trying to view which needs login. But how do I implement this for pages that are not login protected?
Surprisingly, I used the "session" object in Flask to solve the problem.
I was under the impression that "session" comes into play only after user has logged in, but apparently not.
When user clicked to register their response, before redirecting them to sigin page I stored the current url in the session storage like:
session['prev_url'] = request.url
Then on the signin page, after successful signin, I checked the 'prev_url' key in session, if it exists, I redirected to that page else I redirected to home page. Something like:
if session.get('prev_url') is not None:
return redirect(session.get('prev_url'))
return redirect(url_for('index'))

Restricting POST request access to only from HTML Form

In my Django app, I have an html page with a button, "Add user". This add user button makes a post request to /api/add-user. How can I restrict it so that not anyone can just ping /api/add-user so that you can only do it from the form submission in the html page? Thanks!

Django URL : Redirect to a url having input from a form

I want the user after its registration, to redirect to a page with URL containing his/her name. I tried passing it as input in urls.py and passing name an variable in the view but its showing name variable insted of the person's name. Please help
If you are using Django's authentication and they are logged in after authentication then you will have access to the authenticated user with request.user.
If not then you can pass the primary key through the urls to the next page and get the user from there: user = User.objects.get(pk=user_pk)

How to logout a user using it's session id

I followed this method to have an API to login a user using Tastypie and Django Auth: Login with Tastypie
Once I log a user in through Tastypie, I received a session id I store in my app.
Now I want to logout the suer when he use the logout button -> how can I logout a user based on it's session id ? I wanted to use logout() function but it uses a request containing a user object as parameter and I don't have it with my javascript app.
I tried to find in the code how was made the logout function but it flush the sessionbase and I don't have such an object.
My idea: getting session based on session id and delete the row:
from django.contrib.sessions.models import Session
s = Session.objects.get(pk='2b1189a188b44ad18c35e113ac6ceead')
s.delete()
Is it a good idea ?

After user login, when back is pressed login.html displayed

I have a login form. When login button is pressed user's homepage is displayed. But pressing browser's back button takes control back to login form.
I am using django development server.
I have tried inserting meta tags to prevent cache and django #no-cache
But it doesnt work out
The default django login page can be viewed when logged in. You could decorate the login view function and redirect the user if already logged in.
response = <the_way_you_create_response> (ie direct_to_template, HttpResponse)
response['Cache-Control'] = 'no-cache, no-store'
return response
and of course you need to check request.user.is_authenticated() in the view (and do the redirect if value is True)
In your login page you need to check if a users session exists and if it does then you should redirect the user to his homepage.
However, this can't be done with a HTML page so you need to use some kinda server side scripting language(PHP, ASP, JSP Etc) for your login page.