我在尝试使用包含异常字符的 slug 字段时收到一条错误消息

I have an error msg trying to use a slug field that contains an unusual character

我的数据库 (MariaDB) 设置为 utf8mb4,我的模型和视图都定义为使用 unicode 字符。 记录的创建是正确的,并且使用例如 corée 的值正确创建了一个 slug。问题在创建后立即出现,带有 NoReverse 匹配错误消息。我查看了文档,但我不明白我做错了什么。感谢您的支持。

NoReverseMatch 在 /masterdat/countrycode/ 反向 'countrycode_update' 关键字参数 '{'slug': 'corée'}' 未找到。尝试了 1 种模式:['masterdat/countrycode/(?P[-a-zA-Z0-9_]+)/$'] 请求方式:GET 请求 URL:http://127.0.0.1:8000/masterdat/countrycode/ 姜戈版本:3.2.5 异常类型:NoReverseMatch 异常值:
反向 'countrycode_update' 关键字参数 '{'slug': 'corée'}' 未找到。尝试了 1 种模式:['masterdat/countrycode/(?P[-a-zA-Z0-9_]+)/$']

URL:    
from django.urls import path

from masterdat import views
from masterdat.views import CountrycodeListView
from masterdat.views import CountrycodeCreateView, CountrycodeUpdateView 

urlpatterns = [
    path('masterdathome/', views.masterdathome, name='masterdathome'),
    path('countrycode/', CountrycodeListView.as_view(), 
    name='countrycode_list'),
    path('countrycode/add/', CountrycodeCreateView.as_view(), 
    name='countrycode_add'),
    path('countrycode/<slug:slug>/', CountrycodeUpdateView.as_view(), 
    name='countrycode_update'),    
]

Model:
class Countrycode(models.Model):
    cou_code = models.CharField(
        "Country name", max_length=40, primary_key=True)
    cou_recblocked = models.BooleanField("Country record blocked")
    cou_creator = models.ForeignKey(
        User, on_delete=models.PROTECT, related_name='+', 
    verbose_name="Created by")
    cou_creationdate = models.DateTimeField("Creation date", 
    auto_now=True)
    cou_modifier = models.ForeignKey(
        User, on_delete=models.PROTECT, related_name='+', 
    verbose_name="Last modification by")
    cou_modified = models.DateTimeField(
        "Last modification date", auto_now=True)
    slug = models.SlugField(max_length=50, allow_unicode=True)

    class Meta:
        ordering = ["cou_code"] 

    def __str__(self):
        return self.cou_code

    def get_absolute_url(self):
        return reverse('countrycode_update', kwargs={'slug': self.slug})

View:
from masterdat.models import Countrycode
#-----------------------
# List view Management
#-----------------------
class CountrycodeListView(LoginRequiredMixin, PermissionRequiredMixin, ListView):
    permission_required = 'masterdat.view_countrycode'
    
    model = Countrycode
    paginate_by = 10

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        return context

# Search bar
    def get_queryset(self):
        result = super(CountrycodeListView, self).get_queryset()

        query = self.request.GET.get('q')
        if query:
            query_list = query.split()
            result = result.filter(
                reduce(operator.and_,
                    (Q(cou_code__icontains=q) for q in query_list))
            )

        return result
#-----------------------------------
# Create and Update Views Management
#-----------------------------------
from masterdat.forms import CountrycodeForm

# Create View -----------
class CountrycodeCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
    permission_required = 'masterdat.add_countrycode'

    model = Countrycode
    form_class = CountrycodeForm
    success_url = reverse_lazy('countrycode_list')

    def form_valid(self, form):
        form.instance.slug = slugify(form.instance.cou_code, allow_unicode=True)
        form.instance.cou_creator = self.request.user
        form.instance.cou_modifier = self.request.user
        form.save
        return super().form_valid(form)

# Update View -----------
class CountrycodeUpdateView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
    permission_required = 'masterdat.change_countrycode'

    model = Countrycode
    form_class = CountrycodeForm
    success_url = reverse_lazy('countrycode_list')

    def form_valid(self, form):
        form.instance.cou_modifier = self.request.user
        form.save
        return super().form_valid(form)

正则表达式 (?P[-a-zA-Z0-9_]+) 只会匹配“cor”,因为“é”不在“A-Z”或“a-z”范围内。

您可以将其更改为 (?P[-\w0-9_]+) 以匹配非 ASCII 字符。