no data is retrieved after query

Viewed 47

I am trying to fetch specific records from the database table based on user input but getting no data in the objj. Can anybody specify the error? objects.all() is also returning no data.

views.py

from django.views.generic import TemplateView, ListView, DetailView
from ssr.models import dinucleotides
from ssr.forms import InputForm



# Create your views here.
def homepage(request):
    return render(request,'index.html')

def searchpage(request):
  if(request.method == 'POST'):
    fm=InputForm(request.POST)
    if fm.is_valid():
      print('form validated')
      Motiff = fm.cleaned_data['Motiff']
      
      obj1=dinucleotides.objects.filter( SSRtype=Motiff)
      objj={'obj1':obj1 }
    return render(request,'result.html', objj)

    
 
  else:
    fm=InputForm()
    return render(request,'search.html',{'form':fm})```



# models.py

from django.db import models


class dinucleotides(models.Model):
    ID = models.IntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    Chromosome = models.CharField(db_column='Chromosome', max_length=100, blank=True, null=True)  # Field name made lowercase.
    SSRtype = models.CharField(db_column='SSRtype', max_length=100, blank=True, null=True)  # Field name made lowercase.
    Sequence = models.CharField(db_column='SSRsequence', max_length=10000, blank=True, null=True)  # Field name made lowercase.
    Size = models.IntegerField(db_column='Size', blank=True, null=True)  # Field name made lowercase.
    Start = models.IntegerField(db_column='Start', blank=True, null=True)  # Field name made lowercase.
    End = models.IntegerField(db_column='End', blank=True, null=True)  # Field name made lowercase.

    def __str__(self):
        return self.dinucleotides
1 Answers

Notice how the modules are connected:

from .forms import InputForm
from .models import dinucleotides

Also in the model I return just the string 'dinucleotides'. Wherever there is a prefix like "bboard/result.html" replace "bboard" with your application name. I checked everything is working. Try my code and compare with yours. In the result.html template, the objj dictionary displays the fields of the filtered rows from the database on the page.

urls.py

from django.urls import path
from .views import *

urlpatterns = [
    path('search/', searchpage),
]

forms.py

from django import forms

class InputForm(forms.Form):
    Motiff = forms.CharField()

models.py

from django.db import models

class dinucleotides(models.Model):
    ID = models.IntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    Chromosome = models.CharField(db_column='Chromosome', max_length=100, blank=True, null=True)  # Field name made lowercase.
    SSRtype = models.CharField(db_column='SSRtype', max_length=100, blank=True, null=True)  # Field name made lowercase.
    Sequence = models.CharField(db_column='SSRsequence', max_length=10000, blank=True, null=True)  # Field name made lowercase.
    Size = models.IntegerField(db_column='Size', blank=True, null=True)  # Field name made lowercase.
    Start = models.IntegerField(db_column='Start', blank=True, null=True)  # Field name made lowercase.
    End = models.IntegerField(db_column='End', blank=True, null=True)  # Field name made lowercase.

    def __str__(self):
        return 'dinucleotides'

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .forms import InputForm
from .models import dinucleotides


def searchpage(request):
    if request.method == "POST":
        fm = InputForm(request.POST)
        if fm.is_valid():
            Motiff = fm.cleaned_data["Motiff"]
            obj1 = dinucleotides.objects.filter(SSRtype=Motiff)
            objj = {'obj1': obj1}
            return render(request, "bboard/result.html", objj)
        else:
            return HttpResponse("Invalid data")
    else:
        fm = InputForm()
        return render(request, "bboard/search.html", {"form": fm})

search.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form</title>
</head>
<body>
<form method="POST" novalidate>
    {% csrf_token %}
    <table>
        {{ form }}
    </table>
    <input type="submit" value="Send">
</form>
</body>
</html>

result.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
</head>
<body>
    {% for aaa in obj1 %}

   <h4>{{ aaa.ID}}</h4>
   <p>{{ aaa.Chromosome }}</p>
   <p>{{ aaa.SSRtype }}</p>
   <p>{{ aaa.Sequence }}</p>
   <p>{{ aaa.Size }}</p>
   <p>{{ aaa.Start }}</p>
   <p>{{ aaa.End }}</p>

   {% endfor %}
</body>
</html>
Related