NoReverseMatch at / Reverse错误与urlresolvers,get_absolute_url()

时间:2017-04-20 12:44:20

标签: python django url-routing

函数get_absolute_url()

中的url reverse()出错

无法反转每个模型中的url(serial_slug),但url本身就存在!

这是主要的url.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^series/', include("serials.urls", namespace='series')),
    url(r'^', include("serials.urls", namespace='homeview')),

]

这是我的app

中的urls.py
from django.conf.urls import include, url
from .views import homeview, post_of_serial, post_of_season, post_of_serie

urlpatterns = [
   url(r'^$', homeview, name='homeview'),  # /series/
   url(r'^(?P<serial_slug>[\w-]+)/$', post_of_serial, name='post_of_serial'),  # /series/Prison_Break/
   url(r'^(?P<serial_slug>[\w-]+)/(?P<season_slug>[\w-]+)/$', post_of_season, name='post_of_season'),  # /series/Prison_Break/season_5/
   url(r'^(?P<serial_slug>[\w-]+)/(?P<season_slug>[\w-]+)/(?P<series_slug>[\w-]+)/$', post_of_serie, name='post_of_serie'),  # /series/Prison_Break/season_5/2/  

]

这是models.py

class Season(models.Model):
    id                                      = models.AutoField(primary_key=True)
    name_of_the_season                      = models.CharField(max_length=40)
    slug                                    = models.SlugField(unique=False, blank=True) 
    name_for_easy_access                    = models.CharField(max_length=40)
    preview_of_season                       = models.ImageField(upload_to="previews/preview_for_season/", default=None,width_field="width_field", height_field="height_field")
    width_field                             = models.IntegerField(default=150)
    height_field                            = models.IntegerField(default=150)
    number_of_released_series               = models.IntegerField(default=0)
    serial_for_this_season                  = models.ForeignKey("Serial", on_delete=models.CASCADE, default=True)
    a_lot_of_series                         = models.ManyToManyField("Series", default=None, blank=True)
    year                                    = models.IntegerField(default=2017)
    timestamp                               = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated                                 = models.DateTimeField(auto_now_add=False, auto_now=True)
    is_active                               = models.BooleanField(default=True)


    def get_absolute_url(self):
        return reverse('series:post_of_season', kwargs=
                      {'serial_slug': self.serial_for_this_season.slug,
                      'season_slug': self.slug,
                      })

    def __str__(self):
        return "%s" % self.name_for_easy_access

    class Meta:
        ordering                            = ["-timestamp"]
        verbose_name                        = 'Season'
        verbose_name_plural                 = 'Seasons'


class Serial(models.Model):
    id                                      = models.AutoField(primary_key=True)
    rus_name_of_seriall                     = models.CharField(max_length=40)
    eng_name_of_seriall                     = models.CharField(max_length=40)
    slug                                    = models.SlugField(unique=False, blank=True) 
    number_of_serial                        = models.DecimalField(max_digits=10, decimal_places=2, default=True)
    preview_of_serial                       = models.ImageField(upload_to="previews/preview_for_serials/", default=None,width_field="width_field", height_field="height_field")
    width_field                             = models.IntegerField(default=0)
    height_field                            = models.IntegerField(default=0)
    seasonss                                = models.ManyToManyField(Season, default=None, blank=True)
    timestamp                               = models.DateField(auto_now_add=True)
    end_or_in_proccess                      = models.ForeignKey(Status, default=None)
    timestamp                               = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated                                 = models.DateTimeField(auto_now_add=False, auto_now=True)
    is_active                               = models.BooleanField(default=True)

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


    def __str__(self):
        return "%s" % self.rus_name_of_seriall

    class Meta:
        ordering                            = ["-timestamp"]
        verbose_name                        = 'Serial'
        verbose_name_plural                 = 'Serials'



class Series(models.Model):
    id                                      = models.AutoField(primary_key=True)
    rus_name                                = models.CharField(max_length=60) 
    eng_name                                = models.CharField(max_length=60) 
    is_active                               = models.BooleanField(default=True)
    serial_of_this_series                   = models.ForeignKey(Serial, on_delete=models.CASCADE, default=True)
    season_of_this_series                   = models.ForeignKey(Season, on_delete=models.CASCADE, default=True)
    number_of_series                        = models.IntegerField(default=0, blank=True, null=True)
    slug                                    = models.SlugField(unique=False, blank=True) 
    description                             = models.TextField(max_length=700, blank=True, default=None) 
    rating                                  = models.FloatField(default=0, blank=True)  
    timestamp_rus                           = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated                                 = models.DateTimeField(auto_now_add=False, auto_now=True)
    timestamp_eng                           = models.CharField(max_length=60) 
    time_of_series                          = models.DecimalField(max_digits=10, decimal_places=2, default=0)

    def get_absolute_url(self):
        return reverse('series:post_of_serie', kwargs=
                      {'serial_slug': self.serial_of_this_series.slug,
                       'season_slug': self.season_of_this_series.slug,
                       'series_slug': self.slug})

    def __str__(self):
        return "%s | %s" % (self.rus_name, self.number_of_series)

    class Meta:
        ordering                            = ["-timestamp_rus"]
        verbose_name                        = 'Series'
        verbose_name_plural                 = 'Series'
  

使用参数'()'和关键字参数反转'post_of_serial'   找不到'{'serial_slug':''}'。尝试过1种模式:   [ '系列/(P [\瓦特 - ?] +)/ $']

     

(lookup_view_s,args,kwargs,len(模式),模式)   django.urls.exceptions.NoReverseMatch:反向'post_of_serial'   参数'()'和关键字参数'{'serial_slug':''}'不是   找到。尝试了1种模式:['series /(?P [\ w - ] +)/ $']

1 个答案:

答案 0 :(得分:0)

从此更改

slug           = models.SlugField(unique=False, blank=True)

到此

slug           = models.SlugField(unique=False)

从所有slug字段中删除“blank = True”,它会起作用!