Django: Session created in database when login page loaded - django

My django_session table was growing very large and it seems to be due to a pingdom bot that I set-up hitting my login page. I tried creating a brand new django 1.4 app and the behaviour is replicated on any page that uses the django.contrib.auth.views.login page, including the default admin login page.
This surely can't be the desired behaviour. Is it a bug? Is there a fix?
(I have redirected the pingdom bot to another page that doesn't cause a new session to be created but I'd like to solve the django issue itself too).
I have seen the question here Huge Django Session table, normal behaviour or bug? and it doesn't seem to be the same issue

After a little bit of searching I found that this is done by the login view (line 55) in the set_test_cookie() method.
In the latest version of Django (git main) it has been removed however: https://github.com/django/django/commit/9d2c0a0ae6ce931699daa87735d5b8b2afaa20f9#django/contrib/auth/views.py
For the time being I would simply recommend you to use a modified version of the login page which doesn't use the set_test_cookie() method.

Related

Django admin page layout changed with new version

I am working on an existing django project where admin console is extensively used.
With my latest run of package upgrade for my application admin page layout has changed.
Models have started appearing on all the pages, leaving a small space to display form fields.
For example this is how form page look like now:
Original Form view was occupying the whole page like this:
Any clue on what has been changed and which setting to change to get back the original view?
From the django-3.1 release notes,
The admin now has a sidebar on larger screens for easier navigation.
It is enabled by default but can be disabled by using a custom
AdminSite and setting AdminSite.enable_nav_sidebar
to False.
You can refer Customizing the AdminSite class - (django doc) to know more about customizing the AdminSite
Try this, open your browser and clear all the history then login back in again and it should work. I am sure the browser has cached an old version of Django admin.
I had the same problem when using version 3.1. I started a new demo app with version 2.2 to demonstrate something and when I got back to version 3.1 I got that issue, So what I did was just clear the browser history and everything came to its original state.

Magento 2 cannot login admin because form key invalid

I'm in a brain-breaking problem here. I've created a nice Magento 2.1.7 installation with two websites (two stores, two domains) and somehow after a while we are unable to login the backend of Magento.
Invalid Form Key. Please refresh the page.
Now, after trying a few options, still no success. Checked core_config_data, edit max_input_vars, nothing works. The strange thing is, on the front it's still possible to checkout and do your shopping.
Anyway, I'm stumped here. Why is this happening, and how can this be fixed. Any help is welcome.
I often get this error, clearing the cookies for that site, then opening the admin panel in a new tab will resolve this.
You can recreate this error message if you double click the login button after filling out your login information.
Here are a few other reasons you may experience this issue
The form key inputs are outside of the form, you should check the html on the admin panel to check these inputs and exists and have correct values
Make sure the link you use to get to the admin page does not already have a form key in the url
Does this issue generate anything in var/log/system.log ?
Eventually, nothing helped, but what did the trick was creating a new install, setup this new install with all necessary modules (fortunately there were only four), configure the fresh installment like the old one (long live GitHub) and compare your fresh installation to the old one in the database table core_config_data. Basically reproduce your entire setup without any products in it, but just create your stores and categories.
If you spot any differences between the new installment and the old one in the core_config_data, edit them in the old one to be the same as your new one.
Next, edit your env.php with the database credentials to the old database which you've compared and edited.
And the last step, recompile, flush/clean cache and reindex.
Oh, and word of advice, if you're going to use https for your store, setup magento on https. I think something went very wrong when we've changed the base url's from insecure to secure.

How to use react-router and Django templates

Folks,
I am pretty sure I am not the first one to stumble on this problem. But somehow I am unable to find any relevant resources out there.
Here is my issue, I have a backend in Django and my front completely written in Reactjs- React Router - Redux (nice combo right).
when entering the url webhost.com/, django provides me with a page with links to a bundle that is my whole react application and different stylesheets
The problem arise when I want to refresh a page, the browser still tries to query the server even though a route exists in my react-router configuration.
I had a look at the answer here (catch-all option) React-router urls don't work when refreshing or writting manually , but I don't quite understand it and I am afraid to have a new redux state everytime Django will provide the user with a new page.
You can setup up a wildcard url pattern that will render the same view that gets rendered when a request is sent to webhost.com. I don't know if that's going to retain your store though.

Django authentication works on Chrome, but not on Firefox

I'm having a problem where Django's login is working okay on Chrome but not on Firefox: when trying to login to a restricted portion of the site on Firefox, it simply loops back again and again to the login page; furthermore, no error message appears on the log regarding that.
Interestingly, the error doesn't happen when the server is on the local machine.
Does someone have a general idea of what could be causing that strange behavior?
I'm using Django 1.6 on Python 2.7
this isn't much of an answer, but a linking to other similar problems. Because I don't have rep, all I can do is leave an answer.
A issue like this was encountered in 2012 but was never conclusively answered:
Django session doesn't work in Firefox
A similar question where the user could login via local server but not remote firefox was encoutered:
Unable log in to the django admin page with a valid username and password
The second was very well documented and had an accepted answer that was well liked.
Recommendations:
If you are not using https make sure you have this setting SESSION_COOKIE_SECURE = False.
If you are using a database backed, Check if the session is actually being created in the django_sessions table .
If you are using a cached backed, check that SESSION_ENGINE is django.contrib.sessions.backends.cache and that CACHE_BACKEND is properly configured.

dev server hiccups after saving from in Django admin

This is starting bug me: whenever I use the django admin to add or edit a record, I hit save and expect a confirmation page, but the result is a page that tells me the website is experiencing an error (it's not a Django page with a traceback, just a default view in my browser). If I hit reload or back in the browser, it takes me back to the user form. Even though the confimation page doesn't display, it never fails to save my entry in the database, so it's apparently just a problem generating/serving the confirmation page.
Has anyone else had this problem? Am I missing something obvious?
My server: Django dev server
My browser: Internet Explorer 7 (but same problem occurs with any broswer I use)
My OS: Windows XP
I got such a behaiviour when I overwrote __init__ incorrectly. Or to be more precise when switching to a new version of Django with these worng __init__s. That was the day I started to love signals :D.
Can you post your models?
This is certainly not how it's supposed to work! Have you been messing around in the Django codebase at all? The first thing that comes to mind here is a "pdb.set_trace()" left by mistake somewhere in the change_list codepath. Off-hand I can't think what else would cause the server to simply not respond.