Yield Keyword: Django
Yield Keyword: Django
def simpleGeneratorFun():
yield 1
yield 2
yield 3
# Driver code to check above generator function
for value in simpleGeneratorFun():
print(value)
The sets in python are typically used for mathematical operations like union, intersection,
difference
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.
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
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.
STATIC_URL = '/static/'
{% load static %}
<body>
What is Base_DIR
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:
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