Answers for "using slugs in django"

1

slug django

from django.template.defaultfilters import slugify

# in the view maybe while saving the form...
def savedata()
    form = someform(request.POST or None)
  	if form.is_valid():
        new_post = form.save(commit=False) #for holding it before actually saving
        # slugify returns elephant-is-big if passed slugify("elephant is big")
        # and therefore a hyphened slug
        new_post.slug = slugify(form.title) 
        new_post.save()

# model may look like this
class SomeModel(models.Model):
    title = models.Charfield()
    slug = models.SlugField(unique=True)
    ## other fields
Posted by: Guest on July-19-2021
-1

using slug or .. instead of pk in django

from django.shortcuts import get_object_or_404
class TicketDetail(DetailView):
    model = Ticket

    def get_object(self, queryset=None):
        return get_object_or_404(Ticket.objects.all(),uuid=self.kwargs.get("uuid"))
Posted by: Guest on May-01-2021

Python Answers by Framework

Browse Popular Code Answers by Language