'Reverse arguments '(8, '')' not found - how do i pass slug
I am setting up image compression.
def compress(image):
im = Image.open(image)
# create a BytesIO object
im_io = BytesIO()
#resize image
im = im.convert("RGB")
im = im.save(im_io,'JPEG', quality=70, optimize=True)
# create a django-friendly Files object
new_image = File(im_io, name=image.name)
return new_image
class Post(models.Model):
slug = models.SlugField(max_length=100, allow_unicode=True, blank=True)
image = models.ImageField(storage=PublicMediaStorage(), upload_to=path_and_rename, validators=[validate_image])
def save(self, *args, **kwargs):
#image compression start
if self.image:
# call the compress function
new_image = compress(self.image)
# set self.image to new_image
self.image = new_image
#image compression end
super(Post,self).save(*args, **kwargs)
When the user submits the form i get this error
Reverse for 'post-detail' with arguments '(8, '')' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/(?P<slug>[-a-zA-Z0-9_]+)/$']
I believe the error is caused by this line super(Post,self).save(*args, **kwargs) because the slug is not being passed in, however i cannot figure out how to pass the slug.
urls
path('', views.home, name='blog-home'),
path('user/<str:pk_user>/', views.UsersCarsPosts, name='user-posts'),
path('post/new/', views.createPostView, name='post-create'),
path('post/<int:pk>/<slug:slug>/', views.DetailPostView, name='post-detail'),
path('post/<int:pk>/<slug:slug>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/<slug:slug>/delete/', PostDeleteView.as_view(), name='post-delete'),
Update:
after i submit the form i check the DB and all of the fields are there however the slug field is empty. Going to keep digging
Update:
I am dumb and didn't realize I had two save methods on the model.
Solution 1:[1]
(?P<slug>[-a-zA-Z0-9_]+ means that the slug must be a string of one or more characters. However, you are passing ''. You need to figure out why the slug is empty.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Code-Apprentice |
