Posts

Showing posts with the label django

Interview questions and answers

Ques: How are the functions help() and dir() different? Ans: These are the two functions that are accessible from the Python Interpreter. These two functions are used for viewing a consolidated dump of built-in functions. help : It will display the documentation string. It is used to see the help related to modules, keywords, attributes, etc. To view the help for a keyword, topics, you just need to type, help("<keyword>") dir : Without arguments, it will return list of names in the current local scope.  With an argument, it will return a list of valid attributes for that object. Ques:  Which statement of Python is used whenever a statement is required syntactically but the program needs no action? Ans: p ass is no-operation / action statement in Python. If we want to get data from a model  and even if the requested data does not exist, we want to continue with other tasks. In such a scenario, use try-except block with pass statement i...

How sessions work in Django?

The session framework stores and retrieves data. It stores the data on the server side and sends and receives the cookies. Cookies contain a session ID, not the data itself unless we are using the cookie-based sessions. Django provides support for anonymous sessions. Configuring the session engine: By default, Django stores sessions in our database. We can configure Django to store session data using these sessions engines: Database-backed sessions Cached sessions File-based sessions Cookie-based sessions Database-backed sessions: To use databased-backed sessions we need to add "django.contrib.sessions" to our INSTALLED_APPS setting. After this configuration, we need to run manage.py migrate to install the database table that stores session data. Cached sessions: For better performance, we can use a cached-based session backend. We should only use it if we are using Memcached cache backend. File-based sessions: To use file-based sess...

What is middleware in django?

Image
Middleware  : Middleware is a framework of hooks into Django’s request/response processing. Middleware is a lightweight plugin for Django. It allows modifying request or response or the view parameters. Each middleware component is responsible for doing some specific function. For example, Django includes a middleware component, AuthenticationMiddleware, that associates users with requests using sessions. When to use middleware  : We can use middleware if you want to modify the request i.e HttpRequest object which is sent to the view. Or you might want to modify the HttpResponse object returned from the view. Both these can be achieved by using middleware. We might want to perform an operation before the view executes. In such case, we would use a middleware. Hooks and Application order  : Request phase  : During the request phase, before calling the view. Django applies middleware in the order it's defined in MIDDLE_CLASSES top-down. 2 hooks are ava...

Save a file in model without using Django forms

If you want to store the document without using Django forms then you can use below code :  in models.py ... class UserProfile(models.Model) :     user = models.OneToOneField(User)     doc = models.FileField(upload_to='uploads/', blank=True) in python script from core.models import UserProfile from django.core.files import File user1 = UserProfile(pk = 1) user1.doc.save('resume.pdf', File(open('/tmp/resume.pdf', 'r')))