- devsearch - django project
- projects - django app in
djangoproject start project
- templates - html templates with file project with the name of the projects
- One-To-One - One Table record can relate to one record in another table
- One-To-Many - One table record can relate to many records in another table
- using forign key in many table
- Many-To-Many - occurs when multiple records in one table are associated with multiple records in another table
- using a table, intermediary table that records FK in table. Django does it by default
#attribute is attribute of model class
queryset = ModelName.objects.all()
.get()
.get(attribute='value')
.filter()
.filter(attribute__startswith='value')
.filter(attribute__contains='value')
.filter(attribute__icontains='value') #ingore case
.filter(attribute__gt='value') #greater than
.filter(attribute__gte='value')
.filter(attribute__lt='value') #less than and equal to
.filter(attribute__lte='value')
.exclude()
.exclude(attribute='value')
.filter().order_by('key1', '-key2') #- equals descending
.create(attribute='value') # Create instance of model
.save() # save instance of model
.attribute = "New Value"
.last()
object.delete()
object.childmodel_set.all()
object.relationshipname.all()
{{form.as_p}}
form.field
form.field.label
{% for field in form %}
- can also update form types by overwriding constructor for modelforms and updating fields
-
create static directory and sub directories images, styles, and js in base_dir
-
update STATICFILES_DIR variable in settings.js
-
when using static files you must load static files
{% load static %}
-
Can reference static files like this
<link rel="stylesheet" href='{% static "styles/main.css" %}'>
-
Set This directory in order to provide where to upload image content
MEDIA_ROOT = BASE_DIR / 'static/images'
-
create MEDIA_URL in settings.py and configure MEDIA_ROOT to MEDIA_URL -
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
-
update forms
form = ProjectForm(request.POST,request.FILES, instance=project)
-
update enctype for images
<form action="" method ="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input type="submit">
</form>
- Create 'Static_ROOT' variable in setting.py - defines where static files in production will be
- set this as 'BASE_DIR / 'staticfiles' - run
python manage.py collectstatic
- creates staticfile folder for production
- Will be changed to third party tool in the future
- When Setting
DEBUG=False
- URLS set in production for static is what will show.python manage.py collectstatic
- 'pip install whitenoise` - is required to serve static files
- add the following as middleware
"whitenoise.middleware.WhiteNoiseMiddleware",