urlresolvers . get_absolute_url()

时间:2022-09-04 12:32:56

Error with url reverse() in function get_absolute_url()

函数get_absolute_url()中的url反向错误

can't reverse the url in every models (serial_slug) but url himself is exists!

this is main 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')),

]

this is my urls.py in my app

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/*_Break/
   url(r'^(?P<serial_slug>[\w-]+)/(?P<season_slug>[\w-]+)/$', post_of_season, name='post_of_season'),  # /series/*_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/*_Break/season_5/2/  

]

this is 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'

Reverse for 'post_of_serial' with arguments '()' and keyword arguments '{'serial_slug': ''}' not found. 1 pattern(s) tried: ['series/(?P[\w-]+)/$']

返回'post_of_serial'和'{'serial_slug': '}'的参数'()'。1模式(s)尝试:['系列/(? P[\ w -]+)/美元)

(lookup_view_s, args, kwargs, len(patterns), patterns) django.urls.exceptions.NoReverseMatch: Reverse for 'post_of_serial' with arguments '()' and keyword arguments '{'serial_slug': ''}' not found. 1 pattern(s) tried: ['series/(?P[\w-]+)/$']

(lookup_view_s、args、kwargs、len(patterns)、patterns)NoReverseMatch:使用参数'()' post_of_serial'和关键字参数'{'serial_slug': '}'未找到。1模式(s)尝试:['系列/(? P[\ w -]+)/美元)

1 个解决方案

#1


0  

Change from this

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

to this

slug           = models.SlugField(unique=False)

delete from all slug fields "blank=True" and it's gonna work!

#1


0  

Change from this

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

to this

slug           = models.SlugField(unique=False)

delete from all slug fields "blank=True" and it's gonna work!