Django-Basics Flashcards
Command to start a project
django-admin startproject project_name .
How to create a new django app?
python manage.py startapp app_name
What are the bolierplate files generated when you create a django project?(6)
project_name
- __init__.py : just indicating that it is a package
asgi.py: provides asynchronous server gateway interface
wsgi.py: provides webserver gateway interface
urls.py: points to various url mapping in different apps within the projects
setting.py : contains all the configuration applied on the p[roject
manage.py : contains various commandline function executed during dev of a project.(ex like creating a ap,migrating.. etc…)
command to run the django dev server?
python manage.py runserver
What are the bolierplate files generated when you create a new django app?(6+1)
total: 6+1 files.
app_name
__init__.py: indicate this as package so that it can be imported in other appications
models.py : contains all django models
apps.py:configuration file for the app itself
views.py : contains all the views
admin.py: configuration file for django admin.
tests.py:contains the tests
migrate : all migration files will be stored here
__init__.py
How to add a new app to a django project?
- create the app
python manage.py startapp app_name
2.Add to the list of installed app in django project.- Open settings.py in main project directory.Insert the following entry to a list called “INSTALLED_APPS”
- “app_name.app.App_nameConfig” (Last part shoul;d be the app_name in camel case and whole netry must be enclosed within a string.(why check this out??))
How to update the database when new models are added?
Lets the app name be app_name
steps to follow:
1. python manage.py makemigrations app_name (name of the app is oiptional.It’s best practivce toa add itwhy?? seperately mentioned in the migration files??)
2.python migrate
How to create a rootuser from cli?
python manage.py createsuperuser
How to register an model to admin?
1.Open admin file within the respective app
2.add following entry
from django.contrib import admin
admin.site.register(Model_name)
How to import ListView?
from django.views.generic import ListView
let the projet directory be as follows
main_app
sub_app
-> subapp contain a view called BookListView add this to the path pof the project.
from django.urls import path,include
from django.contrib import admin
urls = [
path(“admin/”,admin.site.urls),
path(“books/”,include(“books.urls”)) # note that the module is passed as string.again why????doe sboth of them work????
]
————————————-
sub_app/urls.py
—————————————
from django.urls import path
from .views import BookListView
urlpatterns=[
path(““,BookListView.as_view(),name=’book_list’)
]
—————————————
How to create a view object poining to a model and a templeate./give one example class?create a booklist view
from django.views.generic import ListView
class BookListView(ListView):
model=Book
template_name=”booklist_template.html” # again passed as string why???
Where will the django look for templates by default.?
Assume there is an app called book
Django first check for template file within templates/book/template_name.html within book directory first
Then it checks for templates/book/template_name.html within main project directory.
If we have a Listview with modelname book how can we access this model details from it’s template files?(objact name to be used)
default name is [model_name]_list.So for the current ex obj name will be book_list
How to use for loop ithin templates.Show an example?
{% for book in book_list%}
<h3>Book Name {{book.title}}</h3>
<h3>
{%% endfor %} #Do nopt forget to close the loop
----------------------------------------------------------</h3>
Cli command to run tests in django?
python manage.py test
What is reverse() used for?
reverse function takes a string as an arg and return the path (as string?? check this) with passed argument as it’s name.
While creating tests how to create the database object first in django?
Every test object must inherit from TestCase object(from django.test import TestCase)
Inside test obj create a class method setUpTestData() to set up the data.Following example gives a clear idea
——————————————
class BookTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.book= Book.objects.create(…) # book is one of the models that is imported to this file.
def test_booktest(self): #ex test .Not that all the test must start with test_
self.AssertEqual(self.book.title,”dsnmkjd”) #Note that the book obj defined earlier is accessible using self.Assertion methods are availbale as inherited methods from TestCase obvject.
How to set a url for a model?
Override get_absolute_url function in model return reverse(namespace:url_name,args)
What does unique_for_date arg in slug field accomplish?
Lets assume the following field
slug=models.SlugField(unique_for_date=’publish’)?
Do we need to call makemigrations and migrate after this?
After applying the above argument slug and publish date must be unique for all the elements in the model.
The entry does not make any changes on the internal database.The whole restriction is applied at django level.Although e have to still apply migrations to save the model changes so the it is enforced by the django.