We will learn how request.get method works by making the function which is aiming at sort blogs by different category. BUt in this project we will use different members as the different blog categories. Project link

1. Create category in db / models.py


from django.db import models

class Caselog(models.Model):
    caseid = models.CharField(null=True, blank=True,max_length=200)
    orgnization = models.CharField(null=True, blank=True,max_length=200)
    problem = models.CharField(null=True, blank=True,max_length=200)
    solution = models.TextField(null=True, blank=True)
    # handleby = models.CharField(max_length=100, default='',
    #             choices=((u'Andy',u'Andy'),(u'David',u'David')))
    MEMBER = (
        ('david','David'),
        ('andy','Andy'),
    )
    handleby = models.CharField(null=True, blank=True, max_length=5, choices=MEMBER)
    date = models.DateField(null=True)
    def __str__(self):
    return self.caseid

Explanation about the codes above. choices is a dropdown list .

MEMBER = (
    ('david','David'),
    ('andy','Andy'),

lower letters are the attribute that will get in url, and the capital letters will be shown in the admin console when we create a new caselog and could chose a member by the dropdown list.

Then, evenytime, if any changes in the models.py, run the command below.

python manege.py makemigrations
python manage.py migrate

2. views.py

from django.shortcuts import render
from webapp.models import Caselog

def default(request):
    print('==='*30)
    print(request)
    print('==='*30)
    print(dir(request))
    print('==='*30)
    print(type(request))
    print('==='*30)
    queryset = request.GET.get('handleby')
    print(queryset)
    if queryset:
        caselog_list = Caselog.objects.filter(handleby=queryset)
    else:
        caselog_list = Caselog.objects.all()
    context = {
        'Caselog': caselog_list
    }
return render(request, 'default.html', context)

The explanation.

3. templates

Let’s make a html button to input data sorting attribute link.

<div class="ui thin visible left sidebar inverted vertical menu" id="sidebar">
      <div class="item">
        <a class="item" href="/default">Index</a>
      </div>
      <div class="item">
        <a class="item" href="?handleby=david">David</a>
      </div>
      <div class="item">
        <a class="item" href="?handleby=andy">Andy</a>
      </div>
      <div class="item">
        <a class="item" href="/admin">Admin</a>
      </div>
</div>

yay!!! finished.

Tipping