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?