Home>

I want to create a web application with Django. Django documentation First Django app and similar tutorials Has finished a few, and is currently studying python classes to implement the process.
Could you tell me about the following points?


When creating a web application, shouldn't the variable (instance name) for creating an instance have a different name for each user?

If multiple users execute processing on the server side, (probably) multiple instances will exist at the same time. If I can't create an instance with a unique name for each user at this time, I'm confused. Is this idea correct?


If the above idea is correct, I do not know how to create an instance with a unique name for each user.

Class Sample:
  pass
user_neme = hoge
hoge_class = Sample


Is there a way to write like this?


If question 1 is correct and question 2 is impossible or inefficient, how is it solved?

Thanks for your cooperation.

  • Answer # 1

    If you use a dictionary, you can access the instance by "dictionary name [name]".

    class Sample:
        def __init __ (self, name, date):
            self.name = name
            self.date = date
    users = {}
    users ["hoge"] = Sample ("hoge", "1995-12-31")
    users ["fuga"] = Sample ("fuga", "1983-01-25")
    print (users ["fuga"]. name)
    print (users ["hoge"]. date)

  • Answer # 2

    Use a list of classes to dynamically create and delete instances
    You can add/remove elements freely, and individual instances can be accessed by index