Learning Django from Udemy Course [Demo Website of BT real estate]
Run python manage.py startapp <app_name>
command where the app needs to be created.
Make sure to add <app_name> to INSTALLED_APPS list in settings.py of the project
- For each app created, a separate urls.py file have to be created for navigating in the app.
- Path of every urls.py file in an app should be added to the urls.py of the project.
- Path to the templates directory need to be specified in TEMPLATES list in settings.py of the project.
- Above points helps in accessing the templates anywhere inside the app
- In order to preload all static files run
python manage.py collectstatic
command - The above command copies all static files from all static directory of apps in to the
static
dir of the project. - Created a path with
STATIC_ROOT
string variable andSTATICFILES_DIRS
list in settings.py of the project.
- Add components of webpage in
base.html
by linking it to the filestemplates/partials
dir {% load static %}
to load all static files in current html{% include 'partials/_topbar.html'%}
to load the contents of_topbar.html
in current file.
{% url 'index' %}
index in url name specified in urls.py inside pages app.{% if '/' == request.path %}
this template tag checks whether current path is the root path, in order to implement dynamic behaviours inside a webpage. request.path is a string path.
psycopg2
pip package is used to connect to postgress database from python.- New items added in DATABASES dict in settings.py of the project i.e NAME, USER, PASSWORD, HOST and changed the engine name from sqlite3 to postgresql.
- All fields of the model will be created as variable inside models.py of the app.
- Any app can be imported using
import <app_name>
inside the project - Reference to the model fields : https://docs.djangoproject.com/en/3.1/ref/models/fields/
- Once models are created run
python manage.py makemigrations
, it creates a file insidemigrations
directory of the corresponding app. - After running
python manage.py migrate
, we can see the <app_name>_<model_name> table inside the database.
- Import the models created inside models.py of corresponding app.
- Register the model using
admin.site.register(<model_name>)
inside admins.py of the corresponding app.
- Added two variables MEDIA_ROOT and MEDIA_URL inside settings.py of the project.
- Linked MEDIA_URL to urlpatterns inside urls.py of the project using static method and attributes of settings module which is present inside django.conf.
- Cretate base_site.html inside
templates/admin/
directory. This over writes the default wrapper of django. - Add
{% extends 'admin/base.html' %}
as first line inside the new file in order keep all functionalities intact of the admin panel. - CSS of the admin page is overridden by new file created at
<project_name>/static/css/admin.css
.
- In order to customize admin display data of a model, create new class inheriting
admin.ModelAdmin
inside admin.py of the corresponding app. - There are various tuples which provides various functionalities, we can customize display data of the paticular model by overridiing this tuples i.e list_display, list_display_links, list_filter, list_editable, search_fields, list_per_page
- Inside views.py of the corresponding app pass QuerySet obtained from
<model_name>.objects.all()
in the form dictionary to the render method. - This Query set can be used inside <model_name>.html using jinja template.
{{ <variable_name>.<property_name> }}
is used to access the value of a record inside the template i.e html page.- humanize app is used to make properties like price into comma separated values and published date into time passed since published. Reference https://docs.djangoproject.com/en/3.1/ref/contrib/humanize/
- After updating context with paginator, various new attributes gets added to the listings [context].
- We can use order_by and filter method of the objects class for getting custom results from the model. References https://docs.djangoproject.com/en/3.1/topics/pagination/ https://getbootstrap.com/docs/4.1/components/pagination/
- We can import any app from any other app inside the project.
- Using Django ORM extract records from the models and pass it as context to the render method, in order to access the properties of the model inside the webpage using jinja template.
- Added data to single listing in the similar fashion to multiple listings.
- get_object_or_404 method is used to throw 404 when given id doesn't exists in the model.
- Added styling and dynamic data to search.html.
- Linked this search.html file from search button of the index page.
- Fetching search options from inbuilt dictionary based storage i.e choices.py inside listings app.
- When search button is clicked all search parameters with given input values are present in URL path i.e request.GET dictionary object.
- In order to filter the result based on required conditions special paramter format is used inside QuerySet obtained from the model.
<field_name>__<operation>=<field_valuye>
it provides operations like icontains, iexact, ilte, the char 'i' is for case insensitive search.
- In order to preserve the input user entered in the search bar, passed
request.GET
asvalues
inside context dict from views.py of the app. - Added value attribute inside the corresponding tag, this value is used to display on the field.