django-sessions

Django: Anonymous session & “temporary” one-to-one related model?

空扰寡人 提交于 2020-05-13 06:09:46
问题 Currently, I have a Cart model for each registered CustomUser (OneToOne relation): class Cart(models.Model): created_at = models.DateTimeField(auto_now_add = True) user = models.OneToOneField(CustomUser, related_name='cart') But now I have some requirement for anonymous user (guest). after looking for some articles, session seems to be required? Simple database-based session. How to let guest has a related temporary GuestCart model when they need : Because I don't expect even a robot has a

Django/Auth: logout clears the session data?

泪湿孤枕 提交于 2020-05-12 19:50:31
问题 I would like to know if auth.logout clears session data or i have to do it by my self. from django.contrib.auth.decorators import login_required from django.contrib import auth @login_required def logout(request): auth.logout(request) return redirect('base:homepage') Something like this... from django.contrib.auth.decorators import login_required from django.contrib import auth @login_required def logout(request): for sesskey in request.session.keys(): del request.session[sesskey] auth.logout

Django session doesn't work in Firefox

倾然丶 夕夏残阳落幕 提交于 2020-01-11 09:25:35
问题 I have a search view that saves some data in a session for another view: def search(request): ... if request.method == 'POST': form = SearchForm(request.POST) if form.is_valid(): cd = form.cleaned_data melk=data(cd) request.session['data'] = melk request.session['form'] = form return redirect('/result/') ... def result(request): ... melk_list = request.session['data'] form = request.session['form'] ... When I use Chrome, everything is good. But when I use Firefox, it works fine the first time

Django: How to trigger a session 'save' of form data when clicking a non-submit link

我怕爱的太早我们不能终老 提交于 2020-01-06 05:08:06
问题 I know if want you to store form information to the session during a submit you do it with the 'def post' in your view. However, I can not figure out how to store form information when a random link e.g. 'homepage'. How would you go about storing form information when a user clicks away from the form? 回答1: To store information in the session, you don't really need to post, or submit some kind of form. You can do it anywhere, where you have request using session attribute. the session is dict

How do I properly unit test a Django session?

北慕城南 提交于 2020-01-04 02:43:08
问题 The behavior of Django sessions changes between "standard" views code and test code, making it unclear how test code is written for sessions. Googling this yields two relevant discussions about this issue: Easier manipulation of sessions by test client test.Client.session.save() raises error for anonymous users I'm confused because both tickets have different ways of dealing with this problem and they were both Accepted. I assume this means they were patched and the behavior is now different.

Django add to cart and cart view error

霸气de小男生 提交于 2019-12-31 07:22:09
问题 I get a 'NoneType' object is not iterable error when I add 1 object to the cart via the scan_to_cart view and want to add a second object. Also I get the same error when I want to view my cart when there are actually objects in it. I could not find a common problem with a solution... Is the python version I work with the issue, or is there a logic or code error? Thanks in advance for suggestions/advice! models: from manageinv.models import Child User = settings.AUTH_USER_MODEL class

Modify session data of different Django user

最后都变了- 提交于 2019-12-21 23:05:16
问题 This may not be possible, but when certain conditions happen, I'd like to modify the session data of certain logged in users (flagging that some extra logic needs to run the next time they load a page). Is there a way to access the session of a user by their ID? 回答1: tldr; Query Session model, then modify matching sessions via SessionStore . Your question is twofold, how to get session of a user, and how to modify data of arbitrary sessions (possibly outside of view). Get all logged in user

Delete session key from all users

佐手、 提交于 2019-12-17 19:39:48
问题 When a user logs in some details are saved to the session, lets say using key='user_settings' When some updates happens within the system I need to loop through all users and delete the 'user_settings' key from each users session. That way I will trigger loading the details fresh from the database. How can I loop through all sessions and remove the key from each of the sessions? 回答1: Looping Even if your site has only a few thousand users, you might find that the django_sessions table has a

Django request.session does not resolve

喜你入骨 提交于 2019-12-13 06:17:07
问题 I have a ManyToMany relation between 2 of my models in the same app. This looks like following: class Event(models.Model): eventID = models.CharField(deafult = random_eventID) signal = models.ManyToManyField(Signal) .... .... Now, in my Form (using ModelForm) my eventID field is already populated with the eventID every time i refresh the page (because it gets a new random_eventID every time i refresh the page). This way when, in my forms i select to add a new signal (I want to be able to add