[英]Slug in Korean not working in Django even with 'allow_unicode=True'
I'm trying to make an automatic slug using the slugify in django.utils.text
.我正在尝试使用 django.utils.text 中的django.utils.text
制作一个自动弹头。 The following are my code:以下是我的代码:
# models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils.text import slugify
class Article(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(allow_unicode=True)
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
thumb = models.ImageField(default='default.png', blank=True)
author = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
def save(self, *args, **kwargs):
self.slug = slugify(self.title, allow_unicode=True)
super().save(*args, **kwargs)
def __str__(self):
return self.title
# article_create.html
{% extends 'base.html' %}
{% block content %}
<div class="create-article">
<h2>Awesome New Article </h2>
<form class='site-form' action="{% url 'articles:article_create' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="create">
</form>
</div>
{% endblock %}
# html template - article_lists.html
...
<a href="{% url 'articles:article_details' article.slug %}"></a>
...
I used the allowed_unicode=True in order to allow different languages to be used, but the following error comes up when I type in Korean in the title in the form:我使用 allowed_unicode=True 以允许使用不同的语言,但是当我在表单的标题中输入韩语时出现以下错误:
The first underlined is the title of the article that I'm trying to post, and the second underline is the Django backend checking the slug, but I see that it doesn't recognize the Korean letters... I did what all the other similar StackOverflow, including putting the allow_unicode=True
, but it's not working.第一个下划线是我要发布的文章的标题,第二个下划线是 Django 后端检查 slug,但我看到它不识别韩文字母......我做了所有其他类似的 StackOverflow,包括放置allow_unicode=True
,但它不起作用。 What must be wrong here???这里一定有什么问题???
*edit: This is the urls.py
code: *编辑:这是urls.py
代码:
from django.urls import path
from . import views
app_name = 'articles'
urlpatterns = [
path('', views.article_list, name="article_list"),
path('create/', views.article_create, name="article_create"),
path('<slug:slug>/', views.article_details, name='article_details'),
]
The error is showing what you defined in the urls.py
file for the given route.该错误显示您在urls.py
文件中为给定路由定义的内容。 You could change the route articles/?P<slug>...
to include more than just [-a-zA-Z0-9_]
.您可以更改路线articles/?P<slug>...
以不仅仅包含[-a-zA-Z0-9_]
。 Then the reverse lookup should be able to match the slug.然后反向查找应该能够匹配 slug。
Found out the reason to this!找出原因! it wasn't working it was because of the urls.py
它不起作用是因为urls.py
Before:前:
from django.urls import path
from . import views
app_name = 'articles'
urlpatterns = [
...
path('<slug:slug>/', views.article_details, name='article_details'),
]
After:后:
from django.urls import path
from . import views
app_name = 'articles'
urlpatterns = [
...
path('<slug>/', views.article_details, name='article_details'),
]
I had to change the <slug:slug>
to <slug>
... I really don't know the reason why and maybe it's just for in Korean, but this was the right way for me.我不得不将<slug:slug>
更改为<slug>
...我真的不知道原因,也许它只是用于韩语,但这对我来说是正确的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.