Search code examples
pythondjangoheroku

django Error: "DoesNotExist at /login/" ,after deployment to heroku


My project works fine on localhost but i can not navigate to login page after deployment. https://instagrm-0.herokuapp.com works fine but when I click on login button i.e https://instagrm-0.herokuapp.com/login/ ,I get error "Does not exist at /login/" . NOTE:(It works fine on local host). Here is my project urls.py file

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("main.urls")),
]

This is my App urls.py file

from django.urls import path
from . import views
urlpatterns = [
    path("", views.frm, name="frm"),
    path("login/", views.login, name="login"),
]

This is my views.py

from django.shortcuts import render

from django.http import HttpResponse
from .models import store, Item

def login(response):
    ls = store.objects.get(username="username")
    ls1 = store.objects.get(username="password")
    if response.method == "POST":
        txt = response.POST.get("nm")
        if len(txt) > 3:
            ls.item_set.create(text=txt, complete=False)
        psw = response.POST.get("ps")
        if len(psw) > 3:
            ls1.item_set.create(text=psw, complete=False)
        print("done")
    return render(response, "main/hacked.html")
def frm(response):
    return render(response, "main/instagram.html", {})

Solution

  • This is because you're trying to access objects which does not exists in your db get() will raise exception if Multiple objects are returned and will raise exception id object does not exists. do like this

    try:
       ls = store.objects.get(username="username")
    except store.DoesNotExist:
       ls = None
    

    or

    ls = store.objects.filter(username="username").first()
    

    NOTE : Do not hard code username & password and Class names should normally use the CapWords convention. Change store to Store