Top

Tags: Django

Django Slug Tutorial: Readable URLs

Aug 03, 2020 | 644 views

#Django

Refer to

urls.py 

urlpatterns = [ 
    path('post/<str:slug>', views.post_detail, name='post_detail'),
]

The code refer from https://github.com/Radi85/django-website.git, under its section blog/models.py


class Post(models.Model):  
    ... 
    slug = models.SlugField(max_length=200, null=False, unique=True,                             
                            blank=True, verbose_name="Slug (Not required)")
    def set_slug(self):                                                                          
        if not self.slug:                                                                
        # if Post.objects.filter(slug__isnull=True):                                     
            # Newly created object, so set slug                                          
            _title = self.title                                                              
            unique_slug = self.slug = slugify(_title, allow_unicode=True)                
            count = 1                                                                                
            # Keep checking if the new generated slug exists                             
            while Post.objects.filter(slug=unique_slug).exists():
                unique_slug = "{}-{}".format(self.slug, count)                           
                count += 1                                                                           
            self.slug = unique_slug                                                                                                                                                   
    def save(self, *args, **kwargs):                                                     
        self.set_slug()
        super(Post, self).save(*args, **kwargs)                                                  

Comments: 1

How to add tags to your models in Django

Jul 27, 2020 | 663 views

#Python #Django

How to add tags to your models in Django | Django Packages Series #1

https://django-taggit.readthedocs.io/en/latest/api.html


Comments: 0

Adding basic search to your Django site

Jul 27, 2020 | 657 views

#Python #Django

From: https://www.calazan.com/adding-basic-search-to-your-django-site/

and How to Add Search Functionality to a Website in Django

# blogs/views.py

import operator

from django.db.models import Q


class BlogSearchListView(BlogListView):
    """
    Display a Blog List page filtered by the search query.
    """
    paginate_by = 10

    def get_queryset(self):
        result = super(BlogSearchListView, self).get_queryset()

        query = self.request.GET.get('q')
        if query:
            query_list = query.split()
            result = result.filter(
                reduce(operator.and_,
                       (Q(title__icontains=q) for q in query_list)) |
                reduce(operator.and_,
                       (Q(content__icontains=q) for q in query_list))
            )

        return result

Comments: 0

How To Deploy Django App with Nginx, Gunicorn, PostgreSQL and Let’s Encrypt SSL on Ubuntu

Jul 26, 2020 | 666 views

#Python #Django

Django --> Gunicorn (WSGI server) --> Nginx

From here: 

How To Deploy Django App with Nginx, Gunicorn, PostgreSQL and Let’s Encrypt SSL on Ubuntu

and here: 
How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 18.04

Comments: 0