I'm developing in admin side. And I needs to find user_id. The code
$this->user->getId()
provides no information. Is there any way to get user id.
Thanks.
Here is the best way:
You cannot directly access user_id from database since it is protected.
So you have to access it from session.
$this->session->data['user_id'];
This returns the id of currently logged user.
Related
IS it possible to save the Form data from a Guest / Visitor in the DB?
MY Idea is to display a form where a User is presented with the questions. The answers are unique for each user to I want to map the answers to the Users using a Foreign Key. Problem is that when I want to map it using ForeignKey it says that the answers come from a Null User. I ant my visiting users to access the website also.
I have saved the Data in sessions and then used it
I Do not want to use that approach
Is there any way so that my users can access the website without saving the data in the website. Anyways, the flow of code deletes the answers data from DB once the work is done.
It is not about getting the work done but exploring and getting knowledge.
I can not do
class AnsModel(model.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE)
ans=models.CharField(max_length=1024)
What would be the Model and View for this?
I am currently developing guest checkout in django as I don't want to use django-oscar which gives guest checkout functionality. I searched and got to the conclusion that it can be done through session and got to know that when user logs in the system at that time row will be created in django_session table. So I will have to create manual entry in django_session for my guest checkout. Can anyone please throw some light on how and which will be the best way to do it?
The easiest way it would be to set request.session['user'] to some default value (e.g. guest) by default (you can do
try:
request.session['user']
except KeyError:
request.session['user'] = 'guest'
at the start of every view function (pr functions that can be accessible directly by typing some URL. That's what I've always done and it makes miracles ;). What it actually does is checks whether a user is logged in (request.session has the key user) or not (request.session does not have the key user). When user logs in, set request.session['user'] to his username.
You don't want to touch the django_session table yourself.
Instead, please read
a tutorial about the session framework, or
the more in-depth documentation
The gist of it is that you can store things in the session dict using
request.session['foo'] = True
and they will be transparently persisted using a cookie. You can retrieve them similarly.
I am trying to implement the user ID feature in Google Analytics to track user behaviour across devices. I know that the client ID gets cleared once the user clears the ga cookie.
What happens to the user ID in this case? Is it tied to the authentication system and hence not dependent on the cookie? Can someone throw some light on this?
You have to generate a unique user id for ervery user and store it.
Everytime the user logs in to you site, you have to set correct the user id.
Take a look to this document.
Google Documentation
The id that you will pass should be a unique id from your system that persists.
If the user clears their cookies, it will not matter because the user id will reset once they access a page next time.
According to the Django documentation, the HttpRequest object has a "user" attribute that represents the currently logged in user if the Django installation has activated AuthenticationMiddleware. How does the request object know what the user is? Does the middleware set the user in a cookie and save that cookie to the client browser after the user logs in? In my code, I save the user's ID (from the auth_user table) to a session variable after they've logged in and I usually examine it on each page. If this information is always available in the request object, I shouldn't need to do this. All I should need to do is examine request.user.id. Is this correct?
Thanks.
Yes you've got it, except the user data is stored in the session. You're doing redundant work by keeping track off all that yourself - this something Django is great at!
Check out this documentation on user objects
.. and this article on all of it specifically.
User id is stored not in the cookie but in the session.
And yes, you shouldn't save this data in the session by yourself:
if request.user.is_authenticated():
user_id = request.user.id
If the view should be available for logged users only then instead of checking of request.user.is_authenticated() use the #login_required decorator.
To learn Django, I was making a very basic app which does the following:
Takes a user's login (checks id password in a database).
If user exists and password is right, give user option to either insert,delete or update.
If insert, user can insert an entry into a common table.
Similarly for delete or update.
I was cruising through this but I just got stuck.
My Login page is /index/.
Option for insert/delete/update is at /application/.
Now next, page is displayed according to insert/delete/update at /application/action/
Now the problem is that after completing one insertion, I want to return to /application to carry on my next operation.
But if I do that, I get this error
"Key 'userid' not found in <QueryDict: {}>"
So the view for /application/ is expecting the userid and password in request.POST.
How do I get around this without using external user login modules. I just want a very basic login system just to learn.
Django comes with user authentication built in. I don't think it is external as it is included in django.contrib.
If you use the built in user authentiaction and User model, you will not have to pass the userid to each view. Django will automatically retrieve the logged in user from the session and make it available as a property of the request object.
So using built in user and authentiaction, after logging in a user, you can access that user at
request.user