You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On saving models you can use form save or the create option for the model
1.
apps.py - provides application configuration
app will have its own views that are passed here from base.views.View
urls.py - where your url patterns for the app resides, configuration endpoint with views
path('room/<str:pk>/', views.room, name="room") -- name allows us to change the path without updating the template. use name when loading url href. ex. "{% url 'user-profile' room.host.id %}"
models.py - where your orm for django database will reside
Datatypes
CharField(max_length=200)
TextField(null=True, blank=True)
DateTimeField(auto_now=True) - auto updates date field on text
auto_now_add=True - timestamp set when created
models.ForeignKey(Room, on_delete=models.CASCADE) - adding a foreign key for a many to one relationship established by child model
you can query the foreign key that is connected to the object you query
can fetch set of child as follows: messages = Room.message_set.all()
admin.py - where you register your models to site so you can view it in admin dashboard
forms.py - similar to django serializers, you create meta and specify fields
create user creation form that overrides forms or inherits from them. These can be imported into your views
Templates Directory in Root
Where your template directory is for your html/css
Make sure to update TEMPLATES configuration in project settings.py
Template Inheritence
create a html template and then use include to use it on other html pages
{% include 'navbar.html' %}
Split divs into component htmls that can be included in other page content
main.html where common includes exist
{% extends 'main.html' %}
App templates
Must create template folder/{name of app} - required
Like Templates create root static files with styles and image folder
Create a variable STATICFILES_DIRS = [BASE_DIR / 'static'] in settings.json
load static using following: {% load static %} and must be set for each main template html page to use static folder. everything else will be configured via block content
reference stylesheet similar to url and template <link rel="stylesheet" type='text/css' media='screen' href="{% static 'styles/main.css' %}">
To add images <img src="{% static 'images/image.png' %}">
To add user upload images
Prebuilt Templates
<divclass="form__group"><labelfor="room_topic">Enter a Topic</label>
{% comment %} List topic for dropdown which must match datalist id {% endcomment %}
<inputrequiredname="topic" type="text" value="{{room.topic.name}}" list="topic-list" />
<datalistid="topic-list"><selectid="room_topic">
{% for topic in topics %}
<optionvalue="{{topic.name}}">{{topic.name}}</option>
{% endfor %}
</select></datalist></div>
DRF
Can create a new application or a new folder in existing app called api which includes
views.py -
__init__.py
urls.py - api paths that are added to project urls
serializers.py - similar to model form
pip install djangorestframework and add rest_framework to settings.json of project
Uploading images
PIP install pillow
<form class="form" method="POST" action="" enctype="multipart/form-data"> - enctype must be specified to upload image
form = UserForm(request.POST, request.FILES, instance=user) - request files from image