WEstagram Login Errors

김하성·2021년 2월 4일
0
post-thumbnail

While programming the signup and login features of WEstagram (Instagram clone project), I kept bumping into the same few errors. To avoid making the same mistakes again, here's a brief summary of each error I encountered and why these error exceptions are raised.

404 Not Found: 😡

If the URL in user/urls.py doesn't match the path specificed in the HTTP request, then a 404 Not Found error is raised.

KeyError: 🤬

If a key from the HTTP request is missing or is incorrect (i.e. typo), then a KeyError is raised. Communication between frontend and backend teams is crucial for a program to work! Both frontend and backend teams have to be on the same page on which keys to send and how the keys are spelled (down to the very. last. letter).

TypeError: 🥵

I encountered this error while encoding and decoding hashed passwords in the database. When you're checking whether a password is valid or not using bcrypt or other libraries, you have to make sure that the passwords are the same type. Otherwise, a TypeError is raised. A TypeError is a standard python exception that is raised when an operation is performed on an unsupported object. For example, if I try to run a for loop on an int type, python would raise a TypeError ('int' object is not iterable). I encountered this error while using the bcrypt.checkpw() feature to authenticate user logins. For the bcrypt.checkpw() method to operate successfully, the given passwords (password from request body and password from database) must both be encoded in bytes. Otherwise, a Typeerror is raised.

Few other things to note (지훈 멘토님 감사합니다~):

try:
	data = json.load(request.body)
except:
  • It's good practice to include a try/except block when pulling data from the body of a request, since a body may not necessarily be sent with the request every time

  • Decorators... Decorators are usually used for login/user verification. Since a Django project usually has multiple apps, it would be repetitive to include the same code in different views.py files to authenticate users. To avoid using repetitive code, use a decorator (stored in a different file, such as utils.py) that saves user info in the variable request.user. Honestly, I don't completely understand this concept yet, so we'll see what exactly decorators are needed for once we get started on projects 😅

profile
#mambamentality 🐍

2개의 댓글

comment-user-thumbnail
2021년 2월 4일

하성님도 짤부자시군요..ㅋㅋㅋㅋㅋㅋ
decorator가 이해안될땐? code_sign gogoㅋㅋㅋㅋ

1개의 답글