django - Django urls with UUID issue
问题描述
Initial Error : When CustomerID was CharField in models.py :
after making changes to the model and making the CustomerID UUIDField getting the below error :
I am trying to redirect to the view based on the CustomerID but its not working for UUIDs. It was previously working fine with integers.
Please suggest.
url.py:
from django.urls import path, include
from . import views
urlpatterns = [
path('create/', views.create, name='create'),
path('<uuid:customer_id>', views.detail, name='detail'),
path('search/customers/<uuid:customer_id>', views.detail, name='detail'),
path('customers/<uuid:customer_id>', views.detail, name='detail'),
path('edit/<uuid:customer_id>', views.edit, name='edit'),
path('modify/<uuid:customer_id>', views.modify, name='modify'),
]
views.py
@login_required
def detail(request, customer_id):
customer = get_object_or_404(CustomerMaster, pk=customer_id)
return render(request, 'customers/detail.html',{'customer':customer})
models.py
class CustomerMaster(models.Model):
customerid = models.UUIDField(db_column='CustomerID', primary_key=True) # Field name made lowercase.
customernumber = models.CharField(db_column='CustomerNumber', max_length=50) # Field name made lowercase.
customername = models.CharField(db_column='CustomerName', max_length=50) # Field name made lowercase.
lastmodifiedutc = models.DateTimeField(db_column='LastModifiedUTC') # Field name made lowercase.
lastmodifiedby = models.CharField(db_column='LastModifiedBy', max_length=50) # Field name made lowercase.
active = models.BooleanField(db_column='Active') # Field name made lowercase.
customershortname = models.CharField(db_column='CustomerShortName', max_length=50, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'Customer_Master'
def __str__(self):
return self.CustomerName
解决方案
根据文档,UUID 中的字母必须小写:
uuid - 匹配格式化的 UUID。为防止多个 URL 映射到同一页面,必须包含破折号并且字母必须小写。例如,075194d3-6885-417e-a8a8-6c931e272f00。返回一个UUID实例。
推荐阅读
- python - 在 Python 中使用 async/await 的 Azure ServiceBus 似乎不起作用
- angularjs - '意外的令牌{在函数(
)' 在 .pug 上运行迭代时出现错误 - dataweave - 如果它们不为空,则连接许多字符串的最有效方法
- javascript - Vue:无法将值从父组件传递给子组件
- c# - 创建 SSL 连接时的 .NET Core 3.1 Alpine 图像竞争条件
- kubernetes-helm - 具有多个部署的 helm install 图表 - 并行
- angular - Angular9 - 以模态形式打开反应形式的一部分
- mediawiki - 如何通过 MediaWiki API 获取维基百科所有内容页面的 Pageids 和 Title?
- javascript - 引号中的 Javastript 语法
- android - Flutter - 如何在 Listview 构建器顶部添加项目?