Top

Tags: Linux InstallGuide Django Web-frontend Python Storage HPC Docker k8s Bootstrap

Django Slug Tutorial: Readable URLs

Aug 03, 2020 | 883 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)                                                  

Bootstrap: Align navbar's font with h1, h2, ..

Jul 29, 2020 | 975 views

#Bootstrap

Refer to here.

CSS code: 

h1,
.navbar-brand,
.navbar-nav a {
  font-family: 'Merriweather', serif;
}

check here to look which font you like: CSS Font stack

Bootstrap 4: Autohide navbar when scroll down

Jul 29, 2020 | 1144 views

#Bootstrap

So my code was reference to: Stackoverflow

and its relative code: HERE

CSS

.navbar-tran {
    transition: top 1.0s ease;
}

.navbar-hide {
    top: -56px;
}

HTML:

<nav class="navbar navbar-tran navbar-expand-lg sticky-top navbar-dark bg-dark">
...

jQuery:


/* When the user scrolls down, hide the navbar. 
 * When the user scrolls up, show the navbar */
// document ready
(function ($) {
    
var previousScroll = 56;
    // scroll functions
    $(window).scroll(function(e) {                                                       
    
        // add/remove class to navbar when scrolling to hide/show
        var scroll = $(window).scrollTop();
        if (scroll >= previousScroll+100) {
            $('.navbar').addClass("navbar-hide");
            previousScroll = scroll;

        }else if (scroll+100 < previousScroll) {
            $('.navbar').removeClass("navbar-hide");
            previousScroll = scroll;
        }
    
    });
    
})(jQuery);

For navbar refer to here.

How To Open A Port In CentOS 7 With Firewalld

Jul 28, 2020 | 932 views

#Linux


From: How To Open A Port In CentOS 7 With Firewalld and also here.

And 5 Useful Examples of firewall-cmd command

Open Specific Port

[root@centos7 ~]# firewall-cmd --permanent --add-port=100/tcp
success
[root@centos7 ~]# firewall-cmd --reload
success

We can also open a range of ports in the same way.

[root@centos7 ~]# firewall-cmd --permanent --add-port=200-300/tcp
success

How to add tags to your models in Django

Jul 27, 2020 | 927 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


Jupiter

Jul 27, 2020 | 833 views

#Others

Responsive image

Adding basic search to your Django site

Jul 27, 2020 | 888 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

Bootstrap For Beginners: Navigation Bar - Part Eleven

Jul 26, 2020 | 857 views


From: https://www.c-sharpcorner.com/article/bootstrap-for-beginners-navigation-bar-part-eleven2/

I have started an article series on Bootstrap and published ten articles so far. Read the previous ten parts here,

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

Jul 26, 2020 | 907 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