Hi there, this is easy Django docs with small code snippets which is very helpful to understand whole Django :) Thanks.
[projectname]/ # main project '''personal_portfolio''' ├── [projectname]/ # subfolder '''personal_portfolio''' │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── [projectname]/ # this folder is for templating the project '''projects''' │ │ [migrations] # migrations file goes inside this folder │ │ [static] # all asstes will goes here, like imgs, css,js │ │ [templates] │ │ ├──project_detail.html │ │ ├──project_index.html │ ├──__init__.py │ ├──admin.py │ ├──apps.py │ ├──models.py │ ├──tests.py │ ├──urls.py │ └── views.py ├── [templates]/ │ ├──base.html # define here your layout, so you can extends it in every file. └── manage.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'portfolio', #database name will be here 'USER': 'root', # user name here 'PASSWORD': '', # password here 'HOST': 'localhost', 'PORT': '3306', } }
{% load static %}
- first add this in top of the file
<img src="{% static "my_app/example.jpg" %}" alt="My image">
- then you will get direct access to the static directory
from django.db import models class Project(models.Model): # create a function title = models.CharField(max_length=100) # then add fields description = models.TextField() technology = models.CharField(max_length=20) image = models.FilePathField(path="/img")
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path("projects/", include("projects.urls")), ]
from django.urls import path from . import views urlpatterns = [ path("", views.project_index, name="project_index"), path("/", views.project_detail, name="project_detail"), ]
- Create your views here.
from django.shortcuts import render from projects.models import Project
- show all portfolio page.
def project_index(request): #define the function with request projects = Project.objects.all() # get all data in projects context = { 'projects': projects } return render(request, 'project_index.html', context) # return context as parameters to project_index.html view
- show detail portfolio
def project_detail(request, pk): #define function with request and primary key project = Project.objects.get(pk=pk) # here get the project data by primary key context = { 'project': project } return render(request, 'project_detail.html', context) # return context as project data to the html
# load static here so you can use static assets {% load static %} # Bootstrap CSS <title>Hello, world!</title> # you page content will go inside body {% block page_content %}{% endblock %} # Optional JavaScript
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'projects', #like here I have defined projects ]
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ["templates"], # here add templates path, so you can extends layout file direct in any file }, ]
-> pip install pymysql
Then, edit the init.py file in your project origin dir(the same as settings.py)
import pymysql pymysql.install_as_MySQLdb()