python manage.py startapp pages
Add your app to project: in installed_apps
INSTALLED_APPS = [
'pages.apps.PagesConfig',
]
Install: autopep8
pip install autopep8
TEMPLATES = [
{
# ...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
# ...
},
]
# if os not imported
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static)
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'btre/static')
]
python manage.py collectstatic
<!-- Declaration -->
{% load static %}
<!-- static files -->
<link rel="stylesheet" href="{% static css/style.css %}">
<!-- page links - here index is refer the name declare in views -->
<a href="{% url 'index' %}">Home</a>
base.html
part in separate folder:
base.html
partials
| _navbar.html
| _footer.html
| _topbar.html
<li
{% if '/' == request.path %}
class="nav-item active mr-3"
{% else %}
class="nav-item mr-3"
{% endif %}
>
After Installation Postgres
Config postgres: Open postgres
postgres=# \password postgres
Enter new password:
Enter it again:
postgres=# CREATE DATABASE btredb OWNER postgres;
<!-- list database Check -->
postgres=# \l
<!-- Exit -->
postgres=# \q
Create server from right click on Server
:
Configure postgres with django:
pip install psycopg2
pip install psycopg2-binary
Change setting.py
:
DATABASE = {
'ENGINE': 'django.db.backends.postgres',
'NAME': 'btredb',
'USER': 'postgres',
'PASSWORD': '123456',
'HOST': 'localhost',
}
Run migration: with this postgres database can access the django tables
python manage.py migrate
make migration: after creating models
python manage.py makemigrations
Install Pillow: required for ImageField
pip install Pillow
Install Pillow: required for ImageField
pip install Pillow
python manage.py sqlmigrate listings 0001
- Do again migrate command
python manage.py migrate
It reflects the model changes into database
Creating superuser:
python manage.py createsuperuser
Change Setting:
import os
# Media Folder Settings
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Change project/urls.py
:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
class ListingAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'is_published', 'price', 'list_date', 'realtor')
list_display_links = ('id', 'title')
list_filter = ('realtor', 'is_published')
list_editable = ('is_published',)
search_fields = ('title', 'description', 'address', 'city', 'state', 'zipcode', 'price')
list_per_page = 25
admin.site.register(Listing, ListingAdmin)
pip install pylint-django
Humanize
set settings
INSTALLED_APPS = [
'django.contrib.humanize',
]
Include:
{% load humanize %}
Delete migrations:
python manage.py migrate your_app zero
# This will drop all tables from your_app
# If you want, since you said you want to start over, you can delete your migrations folder, or maybe rename the folder, create a new migrations folder and run
python manage.py makemigrations your_app
python manage.py migrate your_app
-- 1. By dropping the schema
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
In this method, we get all the tables in a schema and delete the tables individually using the DROP TABLE command. This method is suitable for the production environment, as other objects in the schema are retained.
PostgresSQL stores all the tables and the table metadata on its record table called pg_table.
We select all the tables from pg_tables whose schemaName is public. Using subquery, we delete the tables from the schema.
-- 2. By dropping individual tables
SELECT
'DROP TABLE IF EXISTS "' || tablename || '" CASCADE;'
from
pg_tables WHERE schemaname = 'public';