我正在关注Django 教程。相比之下,我有两个数据库——MySql 和 Cassandra。因此,我还需要使用包含UUID类型的 Cassandra 模型。UUID 的形式为 32 个字母数字字符和四个连字符 (8-4-4-12)。因此,我的 urls.py 中有相当复杂的正则表达式:
^([A-Fa-f0-9]{8}))(-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}
Run Code Online (Sandbox Code Playgroud)
在polls/templates/polls/detail.htmlteplate 中是以下行:
<form action="{% url 'polls:vote' question.question_id %}" method="post">
Run Code Online (Sandbox Code Playgroud)
然后将 UUID 类型question.question_id转换为:
/polls/UUID('47de663a-57f2-4ca1-9ad9-81df9ae25973')/
代替
/polls/47de663a-57f2-4ca1-9ad9-81df9ae25973/
因此,我收到了错误消息:
使用参数 '(UUID('47de663a-57f2-4ca1-9ad9-81df9ae25973'),)' 和关键字参数 '{}' 未找到反向'投票'。尝试了 1 个模式:[u'polls/(?P^([A-Fa-f0-9]{8})(-[A-Fa-f0-9]{4}){3}-[ A-Fa-f0-9]{12})/vote/$'
如何处理 UUID 类型?
我想我不能在{% url}标签中使用 str(question.question_id) 函数。
根网址 - mysite/urls.py:
from django.conf.urls import url
from . import views
app_name= 'polls'
urlpatterns = [
#ex: /polls/
url(r'^$',views.index, name='index'),
#ex: /polls/uuid …Run Code Online (Sandbox Code Playgroud)