0

I started using Django recently.

I've integrated MongoDB using PyMongo module [pip3 install pymongo].

My Problem:

Django gives me the same data even after the database gets updated. Whenever I run some manipulations; like comparing the email entered by the user with that of existing email IDs in the database, I get the same result every time.

Looks like the result is getting cached.

Urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.homepage),
    path('register/', views.register),
]

Views.py

from django.shortcuts import render
from pymongo import MongoClient
from django import http

def homepage(request):
    return (render(request, 'index.html'))

def register(request):

    if request.method == 'POST':
        name = request.POST['name']
        email = request.POST['email']
        password = request.POST['password']

        client = MongoClient('mongodb://my_ip:27017/')
        db = client.trainingdb
        collection = db.data
        emailCheck = str(collection.find({"Email":email}))
        if emailCheck == "":
            dbData = {
            'Name': name,
            'Email': email,
            'Password': password
            }
            collection.insert_one(dbData)
            return http.HttpResponse("Success!")
        else:
            return http.HttpResponse("Email exists in database!")
    else:
        return render(request, 'register.html')

The homepage method is for index page or main page. The register method is for the registration page.

Here, I'm trying to check whether the email ID entered by the user pre-exists in the database or not.

Test Cases:

For the first time, I've entered an email ID that's already in the database, I got the expected result: Email exists in database!.

But for the second time, I've entered an email ID that's not in the database; but I get the same old result: Email exists in database!.

Is it due to caching or something like that?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • No, it's not a duplicate. Please read the question twice and understand my problem... – Praneeth Karnena May 02 '18 at 11:42
  • Hi Praneeth Karnena; there is no caching which would make the second query return incorrect results. It's probably best if you double-check your data, and both the code and the query you use to identify whether the item already exists or not; the examples in the question linked by Neil should be useful to you. – Vince Bowdren May 02 '18 at 15:18
  • I have figured out that we can even use `None`. – Praneeth Karnena Feb 01 '19 at 07:47

1 Answers1

0

collection.find() returns a Cursor instance. For some reason you are converting this to a string and comparing it to "". But even if the list is empty, str() of that will not be "", it will be something like "<pymongo.cursor.Cursor object at 0x7f10b69d0c50>". So the condition is never true.

There is no reason to convert to a string or compare with one. Instead, check the number of documents matched:

emailCheck = collection.find({"Email":email}).count()
if emailCheck == 0:
    ...
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895