I am encountering an AttributeError in my Django application while using Djongo as my database backend. The error message is as follows:
AttributeError at /loginPage
'NoneType' object has no attribute 'startswith'
This error occurs in the quote_name method in operations.py. Here is the relevant code snippet:
class DatabaseOperations(BaseDatabaseOperations):
def quote_name(self, name):
if name.startswith('"') and name.endswith('"'):
return name
return '"{}"'.format(name)
I understand that this error typically arises when a method is called on a NoneType object, but I'm unsure why it's happening in this context.
Could anyone please provide insights into why this error might occur and how to resolve it? I'm using Django with Djongo as my database backend.
Additionally, I'm trying to implement functionality where form data goes to two different collections in my MongoDB database, and is working fine. I've attempted to modify the code accordingly, but I'm encountering this error while login. Below is a snippet of the code where the error occurs:
#forms.py
class UserAdminCreationForm(UserCreationForm):
company_name = forms.CharField(max_length=100)
ROLE_CHOICES = [
('admin', 'Admin'),
('user', 'User'),
]
role = forms.ChoiceField(choices=ROLE_CHOICES, label='Role')
email = forms.EmailField(required=True)
class Meta:
model = get_user_model()
fields = ['company_name','role','first_name', 'last_name','email', 'password1', 'password2',]
def save(self, commit=True):
user = super().save(commit=False) # Get the user object without saving to the database yet
user.company_name = self.cleaned_data['company_name']
if commit:
user.save()
slug = slugify(user.company_name)
role = self.cleaned_data.get('role')
collection_name = f"loginapp_{slug}_{role}"
# Change the database table name for the CustomUser model
CustomUser._meta.db_table = collection_name
# Save the user to the new collection
user.save()
# Reset the database table name to the original one
CustomUser._meta.db_table = None
return user
I have noticed that the error occurs intermittently and is resolved when I restart the Django server. Any insights into why this might be happening would be greatly appreciated. Thank you!
Full traceback:
Traceback (most recent call last):
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\handlers\exception.py", line 56, in inner
response = get_response(request)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Krishi Intel\LSPage\loginapp\views.py", line 74, in loginPage
user = authenticate(request, email= email , password=password)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\views\decorators\debug.py", line 42, in sensitive_variables_wrapper
return func(*func_args, **func_kwargs)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\contrib\auth\__init__.py", line 77, in authenticate
user = backend.authenticate(request, **credentials)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\contrib\auth\backends.py", line 46, in authenticate
user = UserModel._default_manager.get_by_natural_key(username)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\contrib\auth\base_user.py", line 47, in get_by_natural_key
return self.get(**{self.model.USERNAME_FIELD: username})
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 646, in get
num = len(clone)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 376, in __len__
self._fetch_all()
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 1867, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 87, in __iter__
results = compiler.execute_sql(
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\compiler.py", line 1385, in execute_sql
sql, params = self.as_sql()
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\compiler.py", line 615, in as_sql
from_, f_params = self.get_from_clause()
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\compiler.py", line 977, in get_from_clause
clause_sql, clause_params = self.compile(from_clause)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\compiler.py", line 506, in compile
sql, params = node.as_sql(self, self.connection)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\datastructures.py", line 199, in as_sql
base_sql = compiler.quote_name_unless_alias(self.table_name)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\compiler.py", line 497, in quote_name_unless_alias
r = self.connection.ops.quote_name(name)
File "C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\djongo\operations.py", line 13, in quote_name
if name.startswith('"') and name.endswith('"'):
Exception Type: AttributeError at /loginPage
Exception Value: 'NoneType' object has no attribute 'startswith'
#views.py
def loginPage(request):
form = LoginForm()
if request.method == 'POST':
form = LoginForm(request.POST, data=request.POST)
if form.is_valid():
email = request.POST.get('email')
password = form.cleaned_data['password']
# Authenticate user using email instead of username
user = authenticate(request, email= email , password=password)
if user is not None:
auth.login(request, user)
return redirect("dashboard")
else:
form.add_error(None, "Invalid email or password")
context = {'loginform': form}
return render(request, 'loginapp/loginPage.html', context=context)