Slug not working

58 views
Skip to first unread message

Aakash Baranwal

unread,
Apr 18, 2019, 5:41:31 AM4/18/19
to django...@googlegroups.com
Hi Everybody,

This is my models.py file:

from django.db.models.signals import pre_save
from django.utils.text import slugify
from django.db import models
from django.conf import settings
from django import forms

# Create your models here.
class Video(models.Model):
    Video_Description= models.CharField(max_length=500)
    slug = models.SlugField(unique=True)

    videofile= models.FileField(upload_to='videos/', null=True, verbose_name="")
    timestamp   = models.DateTimeField(auto_now_add=True)

    class Meta:
    ordering = ['-timestamp']

    def get_absolute_url(self):
    return reverse ("video:detail", kwargs={"slug":self.slug})


    def __str__(self):
        return self.Video_Description + ": " + str(self.id)


def create_slug(instance, new_slug=None):
slug = slugify(instance.Video_Description)
if new_slug is not None:
slug = new_slug
qs = Video.objects.filter(slug=slug).order_by("-id")
exists = qs.exists()
if exists:
new_slug = "%s-%s" %(slug, qs.first().id)
return create_slug(instance, slug=new_slug)
return slug


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

pre_save.connect(pre_save_video_receiver, sender=Video)


The problem I am facing is, slug field appears in the admin and unfortunately in the create page where I am uploading the videos.

However, in the url it is not coming, also in the create page, I have to enter the slug manually. I want it to take whatever input was given to t video_description in the models.py file and it should disappear from the create post page. 

Kindly help me.

With Kind Regards
Aakash Baranwal
Screenshot (7).png
Screenshot (9).png

fofa Zu

unread,
Apr 18, 2019, 6:36:01 AM4/18/19
to django...@googlegroups.com
idk what is the problem but i liked your app it is a tutorial if is it may i get the link thanks

‫في الخميس، 18 أبريل 2019 في 12:41 م تمت كتابة ما يلي بواسطة ‪Aakash Baranwal‬‏ <‪baaka...@gmail.com‬‏>:‬
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALr9hQ2hsoqjBaN7g5%2B6rqkEyRYrfDMHPan8tSvHG%3DQjhnfRbw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Aakash Baranwal

unread,
Apr 18, 2019, 11:23:37 PM4/18/19
to django...@googlegroups.com
Hello Sir,

As of now, I haven't thought of making any tutorials as I am just learning. 

Best
Aakash

Aakash Baranwal

unread,
Apr 19, 2019, 1:53:21 AM4/19/19
to django...@googlegroups.com
Can anyone kindly suggest what I am doing wrong?

Thanks
Aakash

Gabo LaTo

unread,
Apr 19, 2019, 5:02:53 PM4/19/19
to django...@googlegroups.com
Hi Aakash, it' a bit difficult to read it in the email, could you paste it in a pastebin/github or any code snippet share app?

As far as I can see here, you are not adding the slug to the url in your website, so django cannot map your url to your code. It's better if you share a github repository or at least your urls.py.

Cheers,

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages