夹层中的NoReverse Match - 我使用get_absolute_url进行slug字段

时间:2017-02-24 17:58:34

标签: python django mezzanine

我目前正在开发一个夹层项目。我总是得到#No; NoReverseMatch":

Reverse for 'download' with arguments '()' and keyword arguments '{u'slug': u'read-metxt-1-2-3-4-5-6-7-8-9-10'}' not found. 0 pattern(s) tried: []

在我使用Mezzanine之前的应用程序中,没有错误。我只是复制粘贴代码并相应地修改它。

很抱歉提出同样的问题。我知道还有其他类似的问题,但是没有一个是有效的。

models.py

from __future__ import unicode_literals
from django.db import models
from django.core.urlresolvers import reverse
from django.db.models.signals import pre_save
from django.utils.text import slugify
import os

# Create your models here.
class ReportOutline_File(models.Model):
slug = models.SlugField(unique=True, max_length=100)
report_outline = models.FileField()

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

def create_slug(instance, new_slug=None):
    slug = slugify(instance.report_outline.name) #slugifying the title
    if new_slug is not None:
        slug = new_slug
    qs = ReportOutline_File.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
    return create_slug(instance, new_slug=new_slug)

    return slug

def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)
        print instance.slug

pre_save.connect(pre_save_post_receiver, sender=ReportOutline_File)

urls.py(主要:夹层)

urlpatterns += [
   url("^$", direct_to_template, {"template": "index.html"}, name="home"),
   url("^convert-to-ppt/", include("toppt.urls", namespace="txttoppt")),]

urls.py(app)

from .views import (
    converttexttoppt_func,
    download
)
urlpatterns = [
    url(r"^", converttexttoppt_func, name="converttexttoppt"),
    url(r"^download/(?P<slug>[\w-]+)/$", download, name='download')]

views.py

def converttexttoppt_func(request):

form = ReportOutlineForm(request.POST or None, request.FILES or None)

if form.is_valid():
    instance = form.save(commit=False)
    instance.save()
    return HttpResponseRedirect(instance.get_absolute_url())
context = {
    "form": form
}
return render(request, "texttoppt.html", context)

def download(request, slug):
    instance = get_object_or_404(ReportOutline_File, slug=slug)
    #more codes in between....
    return render(request, "download.html", context)

请帮忙!谢谢。我已经为这两天的错误寻找解决方案了! TSK!

1 个答案:

答案 0 :(得分:0)

您提供的子弹包含数字,但网址与数字不匹配

u'read-metxt-1-2-3-4-5-6-7-8-9-10'

因此您需要相应地更新您的网址

url(r"^download/(?P<slug>[\w\d-]+)/$", download, name='download')