Django Flashcards
heroku: To open your app in the browser, type
heroku open
django: To import HttpResponse, type
from django.http import HttpResponse
django: Inside of urls.py you need to import
from . import views
or from .views import *
django: By convention, model names should be
singular
django: App names are usually
the plural of the main model your app will be about.
django: To create database entries from the django shell, type
from app_name.models import Modelname
django: To access the database from within views.py, type
from .models import Model
from .models import *
django: To create a urls.py
from django.urls import path, include
urlpatterns = [
path(‘url’, views.view_name, name=”string”),
]
django: To create templates for an app, you need to first
create a templates folder inside the app and another folder inside the templates folder with the same name as the app.
django: To create templates for an app, you need to first
create a templates folder inside the app.
django: To render a template from a view function, type
return render(request, “app_name/template.html”, {“pass_in_var”: local_var})
django: To tell django where to look for static files
go to settings.py and type
STATICFILES_DIRS = (
os.path.join(BASE_DIR, “assets”),
)
note: must have trailing comma
django: To specify a one to many relationship in a model, type
other_model = models.ForeignKey(OtherModel)
django: When you create a new model and want to include it in the admin area, you must
include it in this apps admin.py file.
from .models import Modelname
admin.site.register(Modelname)
django: To install django, type
pip install django
django: The stub is
the app-like folder automatically created django that holds site wide files.
Html: the form tags must have the two attributes
The type of http method to use (post or get)
The url to send the data to.
django: The python manage.py makemigrations command makes migrations for all of the
installed apps in settings.py
django: In the django shell, before you can add new instances you need to import the model by typing
from app_name.models import Modelname
django: When creating new models, it is usefull to add the __str__ method so that
Modelname.objects.all() returns the names of the objects rather than saying “Modelname Object” if you ever need to iterate through them for a view.
django: When adding new urls to a urls.py file, remember to start it with
url(
django: urls that are assigned in an apps urls.py are
appended to the to the url assigned in the stubs site wide urls.py
django: pk stands for
primary key (it is the id of the record that is automatically generated by django)
django: when creating an html template, model.step_set.all does not require
brackets