0

i wanted to check whether the name is exists in owner table or not.

this is my models.py

class owner(models.Model):
id = models.AutoField
name = models.CharField(max_length=255, blank=True, null=True)
password = models.CharField(max_length=255)

def __str__(self):
    return self.name

this is my index.html `

<form style="color:black" method="POST" action="check" class=" mt-3">
                            {% csrf_token %}
                            <div class="row mb-3">
                                <label for="inputText" class="col-sm-3 col-form-label">Username</label>
                                <div class="col-sm-8">
                                    <input type="text" name="name" placeholder="Username" class="form-control">
                                </div>
                            </div>
                            <div class="row mb-3">
                                <label for="inputText" class="col-sm-3 col-form-label">Password</label>
                                <div class="col-sm-8">
                                    <input type="text" name="password" placeholder="password" class="form-control">
                                </div>
                            </div>
                            <button class="btn btn-success mb-3" type="submit">Login</button>
                            <a class="btn btn-danger mb-3" href="index">Go Back</a>
                        </form>

this is my urls.py

path('index', views.index),
    path('check', views.check),

this is my views.py

def check(request):

    owners = owner.objects.all()
    if request.method == "POST":
        name = request.POST.get('name')
        password = request.POST.get('password')
        if owners.name == name and owners.password == password :
            return render(request, "card/check.html")

it gives error on this line

 if owners.name == name and owners.password == password :

`

how to check whether the name exist or not in table

4
  • What is your model? Commented Dec 9, 2022 at 15:10
  • class owner(models.Model): id = models.AutoField name = models.CharField(max_length=255, blank=True, null=True) password = models.CharField(max_length=255) def __str__(self): return self.name Commented Dec 9, 2022 at 16:08
  • post this as code in the question. Also HINT: what does print(owners) output? What does that mean? Commented Dec 9, 2022 at 19:12
  • Does this answer your question? QuerySet, Object has no attribute id - Django Commented Dec 10, 2022 at 7:39

1 Answer 1

0
owners = owner.objects.all() # This line fetches all the record in the owner table and returns queryset object


if owners.name == name and owners.password == password # The queryset can not have a name attribute. Use an instance.

Solution:

name = request.POST.get('name')
password = request.POST.get('password')
owner = Owner.objects.filter(name=name, password=password).first()
if owner:
    return render(request, "card/check.html")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.