[英]rendered slugs are not correct in url django
Why do I always have an extra %2F
in my generated slugs urls?为什么我生成的 slugs url 中总是有一个额外的
%2F
?
All the slugs are generating correctly below when I print them out in the terminal, but I don't know why the url has the extra %2F
当我在终端中打印它们时,所有 slug 都在下面正确生成,但我不知道为什么 url 有额外的
%2F
Something is wrong somewhere but I cannot seem to spot it某处有问题,但我似乎无法发现它
In my view i am using <a href = "{{ group.get_absolute_url }}">...
to get the slug.在我看来,我正在使用
<a href = "{{ group.get_absolute_url }}">...
来获取 slug。 Now, this works but outputs the above problem.现在,这可行,但会输出上述问题。 If i do
href = "{% url 'group' group.slug %}
this throws an error that it can't find a reverse match.如果我做
href = "{% url 'group' group.slug %}
这会抛出一个错误,它找不到反向匹配。
Example: title of group is a group
the url will be ../%2Fgroup/a-group/
示例:组的标题是
a group
,网址将是../%2Fgroup/a-group/
in urls.py在 urls.py 中
(r'^/group/(?P<slug>[-\w\d]+)/$', "group"),
model模型
class BlogGroup(models.Model):
title = BleachField()
image = models.ImageField(upload_to="uploads/group_images", default="uploads/group_images/none/none.jpg")
created = models.DateTimeField(default = datetime.now)
slug = models.SlugField(unique = True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("blog.views.group", kwargs = {'slug':self.slug})
form形式
class BlogGroupForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BlogGroupForm, self).__init__(*args, **kwargs)
self.fields["title"].requried = True
self.fields["image"].required = True
self.fields["title"].widget = forms.TextInput()
class Meta:
model = BlogGroup
fields = ["title", "image", "slug"]
def save(self, commit = False):
instance = super(BlogGroupForm, self).save(commit = False)
return truncate_slug(instance, BlogGroup)
utils.py实用程序.py
from django.utils.text import slugify
import itertools
def truncate_slug(instance, arg):
length = arg._meta.get_field('slug').max_length
instance.slug = original_slug = slugify(instance.title)[:length]
for x in itertools.count(1):
if not arg.objects.filter(slug = instance.slug).exists():
break
instance.slug = "%s-%d" % (original_slug[:length - len(str(x)) -1], x)
instance.save()
return instance
You have a forward slash at the beginning of your regex.在正则表达式的开头有一个正斜杠。 If you remove it, it should prevent the
%2f
(note that %2f
is a url encoded forward slash).如果你删除它,它应该阻止
%2f
(注意%2f
是一个 url 编码的正斜杠)。
url(r'^group/(?P<slug>[-\w]+)/$', "group", name="group"),
Note that I have also请注意,我也
\\d
, since \\w
already includes digits 0-9\\d
,因为\\w
已经包含数字 0-9url()
(best practice for Django 1.8+) and named the url pattern.url()
(Django 1.8+ 的最佳实践)并命名 url 模式。 That should hopefully make reversing the url with the {% url %}
tag work.{% url %}
标签反转 url 工作。 Using group.get_absolute_url
in your template is fine though, there's no need to use the url tag if you don't want to. group.get_absolute_url
在模板中使用group.get_absolute_url
很好,但如果您不想使用 url 标记,则无需使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.