python 如何修复“”不是有效的UUID,

uqjltbpv  于 2023-06-20  发布在  Python
关注(0)|答案(2)|浏览(163)

我想让用户重新排序/排序表的内容与拖放。当我按下“提交订单”按钮(重新订购后),我有错误:

ValidationError at /save-group-ordering/
['“” is not a valid UUID.']
Request Method: POST
Request URL:    http://127.0.0.1:8000/save-group-ordering/
Django Version: 3.0.2
Exception Type: ValidationError
Exception Value:    
['“” is not a valid UUID.']

Traceback Switch to copy-and-paste view
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site- 
packages\django\db\models\fields\__init__.py in to_python
return uuid.UUID(**{input_form: value}) …
▶ Local vars
C:\Python 3.7\lib\uuid.py in __init__
raise ValueError('badly formed hexadecimal UUID string') …
▶ Local vars
During handling of the above exception (badly formed hexadecimal UUID string), another exception occurred:
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site- packages\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\views\decorators\http.py in inner
return func(request, *args, **kwargs) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\tasks\views.py in save_new_ordering
group = Task.objects.get(lookup_id__exact=lookup_id) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\manager.py in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\query.py in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\query.py in filter
return self._filter_or_exclude(False, *args, **kwargs) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\query.py in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs)) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\sql\query.py in add_q
clause, _ = self._add_q(q_object, self.used_aliases) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\sql\query.py in _add_q
check_filterable=check_filterable, …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\sql\query.py in build_filter
condition = self.build_lookup(lookups, col, value) …
▶ Local vars
C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\sql\query.py in build_lookup

lookup = lookup_class(lhs,rhs)... ▶本地变量C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site- packages\django\db\models\lookups.py ininitself.rhs = self.get_prep_lookup()... ▶本地变量C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\lookups.py in get_prep_lookup return self.lhs.output_field.get_prep_value(self.rhs)... ▶本地变量C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\fields_init.py in get_prep_value return self.to_python(value)... ▶本地变量C:\Users\Admin.2\Desktop\taskmanager\ENV\lib\site-packages\django\db\models\fieldsinit_.py in to_python params='value':值},... ▶本地变量
我只开始使用django,我不明白我做错了什么。
这里是我的models.py

class Task(models.Model):
    
project = models.ForeignKey(Project, null=True, on_delete=models.CASCADE)
title = models.CharField(max_length=300, null=True)
complete = models.BooleanField(default=False)
deadline = models.DateField('Deadline of the task (year, month, day)', default=date.today)
created = models.DateTimeField(auto_now_add=True,null=True)
lookup_id = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True)
order = models.IntegerField(blank=False, default=100_000)

def __str__(self):
return self.title

def deadline_func(self):
days = self.deadline - date.today()
return days

这里是我的urls.py

from django.urls import path
from . import views

urlpatterns = [
path('' , views.projects_list, name="list"),
path('create_project/' , views.createProject, name="create_project"),

path('deadline_project/<str:pk>' , views.deadlineProject, name="deadline_project"),

path('update_project/<str:pk>' , views.updateProject, name="update_project"),
path('delete_project/<str:pk>' , views.deleteProject, name="delete_project"),
path('create_task/<str:pk>' , views.createTask, name="create_task"),

path('save-group-ordering/', views.save_new_ordering, name='save-group-oldering'),

path('deadline_task/<str:pk>' , views.deadlineTask, name="deadline_task"),

path('update_task/<str:pk>' , views.updateTask, name="update_task"),
path('delete_task/<str:pk>' , views.deleteTask, name="delete_task"),  
]

这里是我的views.py

@require_POST
def save_new_ordering(request):
form = OrderingForm(request.POST)

if form.is_valid():
ordered_ids = form.cleaned_data["ordering"].split(',')

with transaction.atomic():
current_order = 1
for lookup_id in ordered_ids:
group = Task.objects.get(lookup_id__exact=lookup_id)
group.order = current_order
group.save()
current_order += 1

return redirect('/')

这里是我的forms.py

class TaskForm(forms.ModelForm):

class Meta:
model = Task
fields = '__all__'

class OrderingForm(forms.Form):
ordering = forms.CharField()

这是我的html

<table class="raz">
                
<tbody id="groups">
{% for task in project.task_set.all %}

                

<tr data-lookup="{{ folder.lookup_id }}">
<td style="width: 50px;"><span class="handle"><img src=" static/images/Up_Down.png " alt="Up_Down_icon"></span></td>

{% if task.complete == True %}
<td style="width: 50px;"class="ch"><input class="checkbox" type="checkbox" name="complete" id="id_complete" checked ></td>
{% else %}
<td style="width: 50px;"><input class="checkbox" type="checkbox" name="complete" id="id_complete"  ></td>
{% endif %}

<td style="width: 550px;"><p>{{task.title}}</p></td>

<td style="width: 50px;"><a class="calendar" href="{% url 'deadline_task' task.id %}">
<img src=" static/images/calendar.png " alt="calendar_icon">
</a></td>

<td style="width: 50px;"><a href="{% url 'update_task' task.id %}" class="edittask">
                            <img src=" static/images/pencil_icon.png " alt="pencil_icon">
</a></td>
                                
<td style="width: 50px;"><a href="{% url 'delete_task' task.id %}" class="deletetask">
                            <img src=" static/images/trash.png " alt="trash_icon">
</a></td>

</tr>

{% endfor%}
</tbody>
                
</table>

<form id="orderingForm" method="post" action="{% url 'save-group-oldering'  %}">
{% csrf_token %}
<input type="hidden" id="orderingInput" name="ordering">
</form>

<button id="saveOrdering">Save ordering</button>
vc9ivgsu

vc9ivgsu1#

因此,当我们在URL中传递id时,就像在这种情况下,UUID将是一个字符串,有时它会获取端点并将其视为UUID,所以我建议您将uuid放在下面的URL。
例如:如果你有两个网址

abc/efg and abc/<str:uuid>

那么顺序应该是

abc/efg
abc/<str:uuid>

如果我们不这样做,那么它将首先读取abc/str:uuid,并将efg视为UUID

r6vfmomb

r6vfmomb2#

要修复uuid问题,只需将id URL放在urlpatterns的末尾,如以下示例:-

url模式= [
path('client/messages/draft/',ComServiceDraftViewSet.as_view(),name=“save-msg-drft”),
path('client/messages/str:message_id/',ComServiceViewSet.as_view(),name=“update-msg”),
]

相关问题