路径failig中的Python 3.6.x/Django 2.0.x UUID
Python:3.6.3//Django:2.0.2 我一次尝试两件新事物。基于类的视图,并使用UUID作为“帐号”路径failig中的Python 3.6.x/Django 2.0.x UUID,django,python-3.x,Django,Python 3.x,Python:3.6.3//Django:2.0.2 我一次尝试两件新事物。基于类的视图,并使用UUID作为“帐号” url.py是: from django.urls import path, re_path from .views.index import * from .views import profile urlpatterns = [ re_path(r'^$', index.IndexDetailView.as_view(),
url.py
是:
from django.urls import path, re_path
from .views.index import *
from .views import profile
urlpatterns = [
re_path(r'^$', index.IndexDetailView.as_view(), name='index'),
path('Members', index.IndexDetailView.as_view(), name='index'),
path('Members/Profile/<uuid:account_number>/', profile.ProfileDetailView.as_view(), name='profile-detail'),
path('Members/Profile/<uuid:account_number>/edit', profile.edit),
path('Members/Profile/create', profile.create),
]
我正在尝试的“DetailView”是:
class ProfileDetailView(DetailView):
model = Profile
pk_url_kwarg = "account_number"
template_name = 'Members/profile2.html'
@verified_email_required
def get_object(self, queryset=None):
queryset = self.get_queryset() if queryset is None else queryset
profile = get_object_or_404(queryset, account_number=self.kwargs['account_number'])
# profile = super().get_object()
return profile
@verified_email_required
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form_readonly'] = True
return context
有一个显示配置文件信息的“部分”视图/模板。它包含一个“编辑”按钮,看起来像:
<div id="profileDetails" class="profileDetails">
<h4>Account Holder Details:</h4>
<table class="profileDetails text-left">
<tbody>
<tr>
<th>Name</th>
<td>{{ profile.last_name }},
{{ profile.first_name }}{% if profile.middle_name %} {{ profile.middle_name }}{% endif %}</td>
</tr>
<tr>
<th>Address</th>
<td>{% if profile.address1 %}
{{ profile.address1 }}<br>
{% endif %}
{{ profile.address2 }}<br>
{{ profile.city }}, {{ profile.state }} {{ profile.zip }}</td>
</tr>
<tr>
<th>Phone</th>
<td>{{ profile.phone }}</td>
</tr>
<tr>
<th>Email</th>
<td>{{ request.user.email }}</td>
</tr>
<tr>
<th>Birthday</th>
<td>{{ profile.birthdate }}</td>
</tr>
<tr>
<th class="smallText">{% if profile.dateModified %}
Last Modified
{% else %}
Date Created
{% endif %}</th>
<td class="smallText">{% if profile.dateModified %}
{{ profile.dateModified }}
{% else %}
{{ profile.dateCreated }}
{% endif %}</td>
</tr>
<tr>
<td colspan="2" class="centerText">
<form class="inlineBlock" method="get" action="/Members/Profile/{{ profile.account_number }}/edit">
{% csrf_token %}
<button id="profileEditButton" class="btn btn-sm btn-default">Edit</button>
</form>
<button id="profileDeleteButton" class="btn btn-sm btn-danger inlineBlock">Delete
</button>
</td>
</tr>
</tbody>
</table>
</div>
我是否需要以不同的方式初始化uuid DB字段?我应该以不同的方式访问它吗?删除连字符
实际上,删除路径中的连字符会导致url.py
失败。错误在于当前路径Members/Profile/7FB2FB245EBD43A4 A2374AD39618719D/edit与这些路径中的任何一个都不匹配。其中“this”是上面的url路径列表
我错过了什么
从Windows进行回溯,从linux进行回溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/Members/Profile/7fb2fb24-5ebd-43a4-a237-4ad39618719d/edit?csrfmiddlewaretoken=yblBLKot54xoB0Laq7fBVIeZARmvWFq7IY6mZ23XrjxbmyU0xZ55VTOqImCmBqvc
Django Version: 2.0.2
Python Version: 3.6.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'auditlog',
'widget_tweaks',
'Members.apps.MembersConfig']
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',
'auditlog.middleware.AuditlogMiddleware']
Traceback:
File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\__init__.py" in to_python
2363. return uuid.UUID(value)
File "C:\Program Files\Python36\lib\uuid.py" in __init__
137. hex = hex.replace('urn:', '').replace('uuid:', '')
During handling of the above exception ('int' object has no attribute 'replace'), another exception occurred:
File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files\Python36\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\allauth\account\decorators.py" in _wrapped_view
32. return view_func(request, *args, **kwargs)
File "C:/Users/Me/PycharmProjects/MyProject/MyApp\Members\views\profile.py" in edit
77. profile = Profile.objects.get(account_number=int(account_number))
File "C:\Program Files\Python36\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py" in get
397. num = len(clone)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py" in __len__
254. self._fetch_all()
File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py" in _fetch_all
1179. self._result_cache = list(self._iterable_class(self))
File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py" in __iter__
53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1051. sql, params = self.as_sql()
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
459. where, w_params = self.compile(self.where) if self.where is not None else ("", [])
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\compiler.py" in compile
391. sql, params = node.as_sql(self, self.connection)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\where.py" in as_sql
80. sql, params = compiler.compile(child)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\compiler.py" in compile
391. sql, params = node.as_sql(self, self.connection)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\lookups.py" in as_sql
161. rhs_sql, rhs_params = self.process_rhs(compiler, connection)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\lookups.py" in process_rhs
260. return super().process_rhs(compiler, connection)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\lookups.py" in process_rhs
93. return self.get_db_prep_lookup(value, connection)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\lookups.py" in get_db_prep_lookup
187. [get_db_prep_value(value, connection, prepared=True)]
File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
2354. value = self.to_python(value)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\__init__.py" in to_python
2368. params={'value': value},
Exception Type: ValidationError at /Members/Profile/7fb2fb24-5ebd-43a4-a237-4ad39618719d/edit
Exception Value: ["'169741278071848279598077364489084760477' is not a valid UUID."]
使用int(account\u number)
将uuid.uuid4()对象转换为一个整数,该整数将中断UUIDField
验证并引发ValidationError
>>> uuid.uuid4()
UUID('e05b17de-061b-4609-b2b5-b84b0168a9dd')
>>> int(uuid.uuid4())
277437837159711504976083722802700941846
因此,不需要将其转换为整数
File "C:/Users/Me/PycharmProjects/MyProject/MyApp\Members\views\profile.py" in edit
77. profile = Profile.objects.get(account_number=int(account_number))
使用int(account\u number)
将uuid.uuid4()对象转换为一个整数,该整数将中断UUIDField
验证并引发ValidationError
>>> uuid.uuid4()
UUID('e05b17de-061b-4609-b2b5-b84b0168a9dd')
>>> int(uuid.uuid4())
277437837159711504976083722802700941846
因此,不需要将其转换为整数
File "C:/Users/Me/PycharmProjects/MyProject/MyApp\Members\views\profile.py" in edit
77. profile = Profile.objects.get(account_number=int(account_number))
您需要发布完整的回溯-不可能知道哪些验证失败。尽管我注意到你几乎肯定不应该在这些方法上使用@verified\u email\u required
,不管是什么方法。@DanielRoseman:添加了回溯你需要发布完整的回溯-不可能知道什么验证失败了。尽管我注意到您几乎肯定不应该在这些方法上使用@verified\u email\u required
,不管是什么。@DanielRoseman:添加了tracebackDoh!我怎么会错过呢,谢谢你!我怎么会错过呢,谢谢你