我最近重置了数据库,现在Django在以登录状态访问任何网站时引发错误['“2” is not a valid UUID.']
。
在我的模型和数据库中,UUID设置为pk,并且已使用超级用户帐户正确登录。
我不知道为什么 Django将2
分配给UUID变量?
型号:
from django.contrib.auth.models import AbstractUser
from django.db import models
import uuid
class CustomUser(AbstractUser):
DID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
def __str__(self):
return self.username
查看:
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from .forms import CustomUserCreationForm
class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
错误:
Error during template rendering
In template C:\Users\Jonas\Desktop\Dashex\Dashex\templates\base.html, error at line 42
“%(value)s” is not a valid UUID.
32
33 <body>
34 {# Navigation Menu #}
35 <header>
36 {% block logo %}
37 <a href="/"><img id="logo" src="{% static 'images/logo.svg' %}" alt="dashex forex dashboard data"></a>
38 {% endblock %}
39 <div class="navigationWrapper">
40 <nav>
41 <ul>
42 {% if user.is_authenticated %}
43 <li><a href="/Dashboard">Dashboard</a></li>
44 <li><a href="{% url 'logout' %}">logout</a></li>
45 {% else %}
46 <li><a href="{% url 'login' %}">login</a></li>
47 {% endif %}
48 </ul>
49 </nav>
50 </div>
51 </header>
52
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/users/login/
Django Version: 3.0
Python Version: 3.8.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'Wiki_app',
'Dashboard',
'users']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 2309, in to_python
return uuid.UUID(**{input_form: value})
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\uuid.py", line 169, in __init__
raise ValueError('badly formed hexadecimal UUID string')
During handling of the above exception (badly formed hexadecimal UUID string), another exception occurred:
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\contrib\auth\views.py", line 63, in dispatch
return super().dispatch(request, *args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\contrib\auth\views.py", line 92, in form_valid
auth_login(self.request, form.get_user())
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\contrib\auth\__init__.py", line 99, in login
if _get_user_session_key(request) != user.pk or (
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\contrib\auth\__init__.py", line 58, in _get_user_session_key
return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 2311, in to_python
raise exceptions.ValidationError(
Exception Type: ValidationError at /users/login/
Exception Value: ['“2” is not a valid UUID.']
已登录用户的数据库UUID条目: