0% found this document useful (0 votes)
67 views4 pages

Yield Keyword: Django

This document contains summaries of common Python and Django concepts: 1. A generator function uses the yield keyword to return a value each time it is called, allowing it to generate a sequence of values. 2. A set in Python is an unordered collection of unique elements. Sets are commonly used for mathematical operations like union, intersection, and difference. 3. The main components of Django's architecture are models (data structure), views (retrieve and format data), templates (display data), and URLs (routing requests).

Uploaded by

satyach123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
67 views4 pages

Yield Keyword: Django

This document contains summaries of common Python and Django concepts: 1. A generator function uses the yield keyword to return a value each time it is called, allowing it to generate a sequence of values. 2. A set in Python is an unordered collection of unique elements. Sets are commonly used for mathematical operations like union, intersection, and difference. 3. The main components of Django's architecture are models (data structure), views (retrieve and format data), templates (display data), and URLs (routing requests).

Uploaded by

satyach123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

What are the generators in Python

A generator-function is defined like a normal function, but whenever it needs to generate a


value, it does so with the yield keyword rather than return. If the body of a def contains yield, the
function automatically becomes a generator function.

# A generator function that yields 1 for first time,


# 2 second time and 3 third time

def simpleGeneratorFun():
    yield 1            
    yield 2            
    yield 3            
   
# Driver code to check above generator function
for value in simpleGeneratorFun(): 
    print(value)

What is Set in Python

a set is a collection of items not in any particular order.

The sets in python are typically used for mathematical operations like union, intersection,
difference

Mention the architecture of Django architecture?


Django architecture consists of

 Models: It describes your database schema and your data structure


 Views: It controls what a user sees, the view retrieves data from appropriate models and
execute any calculation made to the data and pass it to the template
 Templates: It determines how the user sees it. It describes how the data received from
the views should be changed or formatted for display on the page
 Controller: The Django framework and URL parsing

Explain how you can set up the Database in Django?


You can use the command edit mysite/setting.py , it is a normal python module with module
level representing Django settings.
Django uses SQLite by default; it is easy for Django users as such it won’t require any other
type of installation. In the case your database choice is different that you have to the following
keys in the DATABASE ‘default’ item to match your database connection settings
 Engines: you can change database by using ‘django.db.backends.sqlite3’ ,
‘django.db.backeneds.mysql’, ‘django.db.backends.postgresql_psycopg2’,
‘django.db.backends.oracle’ and so on
 Name: The name of your database. In the case if you are using SQLite as your
database, in that case database will be a file on your computer, Name should be a full
absolute path, including file name of that file.

Give an example how you can write a VIEW in Django?

Views are Django functions that take a request and return a response.  To write a view in
Django we take a simple example of “Guru99_home” which uses the template
Guru99_home.html and uses the date-time module to tell us what the time is whenever the
page is refreshed.  The file we required to edit is called view.py, and it will be inside
mysite/myapp/
Copy the below code into it and save the file
       from datatime import datetime
      from django.shortcuts import render
     def home (request):
return render(request, ‘Guru99_home.html’, {‘right_now’: datetime.utcnow()})
Once you have determined the VIEW, you can uncomment this line in urls.py
# url ( r ‘^$’ , ‘mysite.myapp.views.home’ , name ‘Guru99’),
The last step will reload your web app so that the changes are noticed by the web server.

Explain the migration in Django and how you can do in SQL?

 Migration in Django is to make changes to your models like deleting a model, adding a field,
etc. into your database schema.  There are several commands you use to interact with
migrations.
 Migrate
 Makemigrations
 Sqlmigrate
To do the migration in SQL, you have to print the SQL statement for resetting sequences for a
given app name.
django-admin.py sqlsequencreset
Use this command to generate SQL that will fix cases where a sequence is out sync with its
automatically incremented field data

Mention caching strategies that you know in Django!


Few caching strategies that are available in Django are as follows:
 File system caching
 In-memory caching
 Using Memcached
 Database caching
What are the static files

Websites generally need to serve additional files such as images, JavaScript, or CSS. In
Django, we refer to these files as “static files”.
Django provides django.contrib.staticfiles to help you manage them.

Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

In your settings file, define STATIC_URL, for example:

STATIC_URL = '/static/'

Load static files in the templates by using the below expression.

{% load static %}

<body>

<img src="{% static '/wallpaper.jpeg' %}" alt="My image" height="300px" width="700px"/>


</body>

What is Base_DIR

BASE_DIR is your Django project directory. The same directory where manage.py is located

Django Shell

Django Shell is an interactive environment where you can run all your python code along with
code related to Django. The shell will let you to run queries to insert/pull data. In order to invoke
shell, go to your project folder and run the following command:

python manage.py shell

Error handling in Django Forms

raise forms.ValidationError('Looks like a username with that


email or password already exists')

Cookie vs Session in Django

Basically a cookie is Client Side (typically a web browser) Session stores variables on the
Server 

A cookie is something that sits on the client's browser and is merely a reference to
a Session which is, by default, stored in your database.
The cookie stores a random ID and doesn't store any data itself. The session uses the value
in the cookie to determine which Session from the database belongs to the current browser.
This is very different from directly writing information on the cookie.

Example:

httpresponse.set_cookie('logged_in_status', 'True')
# terrible idea: this cookie data is editable and lives on your client's computer

request.session['logged_in_status'] = True
# good idea: this data is not accessible from outside. It's in your database.

Difference

List Comprehension

List comprehensions offer a concise way to create lists based on existing list

hello_letters = [letter for letter in 'hello' if letter != 'l']


print(hello_letters)

This is appending letter to hello_letters list if the condition is true

Newlist = [exprwhichneedtoappend for iter in oldlist if cond]

Squares = [x**2 for x in range(1,100)]

This will give the output:


['h', 'e', 'o']

You might also like