---
# System prepended metadata

title: Repl Django WebShow view.py
tags: [Python]

---

# Repl Django WebShow view.py

###### tags: `Python`

不同欄位各自搜尋

```python=
from django.shortcuts import render
from django.http import JsonResponse
from .models import WebShow
# Create your views here.

def index(request):
	return render(request, 'hello.html', {
		'data': "Hello Django ",
	})

def showdata(request):
    WebShow_list = WebShow.objects.all() # 把所有 WebShow 的資料取出來
    context = {'WebShow_list': WebShow_list} # 建立 Dict對應到WebShow的資料，
    return render(request, 'test.html', context)

def outputdata(data):
  output = {
      "number": data.number,
      "name":data.name,
      "photo":data.photo,
      "style":data.style,
      "level":data.level,
      "color":data.color,
      "cost":data.cost,
      "effect":data.effect,
      "soul":data.soul,
      "attack":data.attack,
      "content":data.content,
      "created_at":data.created_at,
      "date":data.date
      }
  return output

def inputcheck(inputdata,checkdata):
  if checkdata in inputdata:
    return inputdata.getlist(checkdata)
  else :
    return None

def showapi(request):
  print(request.GET)
  number = inputcheck(request.GET,'number')
  name = inputcheck(request.GET,'name')
  style = inputcheck(request.GET,'style')
  level = inputcheck(request.GET,'level')
  color = inputcheck(request.GET,'color')
  cost = inputcheck(request.GET,'cost')
  effect = inputcheck(request.GET,'effect')
  soul = inputcheck(request.GET,'soul')
  attack = inputcheck(request.GET,'attack')

  results = []
  try:
    if number:
      for num in number:
        number_list = WebShow.objects.filter(number__icontains = num)
        for number_data in number_list:
          output = outputdata(number_data)
          results.append(output)
    if name:
      for na in name:
        name_list = WebShow.objects.filter(name__icontains = na)
        for name_data in name_list:
          output = outputdata(name_data)
          results.append(output)
    if style:
      for sty in style:
        style_list = WebShow.objects.filter(style__icontains = sty)
        for style_data in style_list:
          output = outputdata(style_data)
          results.append(output)
    if level:
      for le in level:
        level_list = WebShow.objects.filter(level__icontains = le)
        for level_data in level_list:
          output = outputdata(level_data)
          results.append(output)
    if color:
      for col in color:
        color_list = WebShow.objects.filter(color__icontains = col)
        for color_data in color_list:
          output = outputdata(color_data)
          results.append(output)
    if cost:
      for co in cost:
        cost_list = WebShow.objects.filter(cost__icontains = co)
        for cost_data in cost_list:
          output = outputdata(cost_data)
          results.append(output)
    if effect:
      for eff in effect:
        effect_list = WebShow.objects.filter(style__icontains = eff)
        for effect_data in effect_list:
          output = outputdata(effect_data)
          results.append(output)
    if soul:
      for sou in soul:
        soul_list = WebShow.objects.filter(soul__icontains = sou)
        for soul_data in soul_list:
          output = outputdata(soul_data)
          results.append(output)
    if attack:
      for att in attack:
        attack_list = WebShow.objects.filter(attack__icontains = att)
        for attack_data in attack_list:
          output = outputdata(attack_data)
          results.append(output)
  except:
    pass
  return JsonResponse({"status":True,"total":len(results),"results":results}, safe=False)
```