Dealing with undefined values ​​in code or in a template?

I am writing a web application (in Python, this is not important). One of the features is that people can leave comments on things. I have a class for comments, basically like this:

class Comment:
    user = ...
    # other stuff

where useris an instance of another class,

class User:
    name = ...
    # other stuff

And of course in my template I

<div>${comment.user.name}</div>

Problem: Let me say that I allow people to post comments anonymously. In this case, comment.userthere is None(undefined), and, of course, access to comment.user.namewill lead to an error. What is the best way to handle this? I see three possibilities:

  • - . , , , " " ( "..." ), , . ​​. , , , .
  • user Comment, , undefined. user.name = 'Anonymous' - , .
  • , user.name = Anonymous ( - ), , , . , . (PHPBB?)

, , ( - ) ? , , ? , , .

+3
3

, if .

: . "" .

, . , , ? == 'Anonymous' ?

, , . if user is logged in, display "Logout", otherwise display "Register" and "Login" . .

+1

, . , "" , , . , -, , .

, .

+1

: User, .

None .

0
source

Source: https://habr.com/ru/post/1746813/


All Articles