How To Create Django Python Project
django-admin startproject portfolio
C:\Users\Manibala\desktop\portfolio-project> python manage.py startapp jobs
settings.py-> add ‘jobs’ in install apps
portfolio/urls.py -> add url with function name views.nick and import views
from django.conf.urls import url
from django.contrib import admin
import jobs.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('home', jobs.views.nick, name='home'),
]
jobs/views.py -> write function with request parameter and give path of view page
from django.shortcuts import render
def nick(request):
return render(request, 'jobs/home.html')#to output the render content html
jobs/templates/jobs/home.html
Hello World
Creating models in django: work with database
jobs/models.py
class Job(models.Model): # Model name is Job
# Images
image = models.ImageField(upload_to='images/')
# Summary
summary = models.CharField(max_length=200)
To work with image, install pillow
\Users\Manibala\desktop\portfolio-project> pip3 install pillow or
Pip install Pillow==6.2.1 --no-cache-dir
http://localhost:8000/home running
Postgres set up for django:
https://www.enterprisedb.com/thank-you-downloading-postgresql?anid=1257093
install postgres and create database portfolio db
http://127.0.0.1:53056/browser/
Connecting project to postgres :
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'portfoliodb',
'USER': 'postgres',
'PASSWORD': '****',
'HOST': 'localhost',
'PORT': '5432',
}
}
-----------------
$pip install psycopg2
C:\WINDOWS\system32> pip install psycopg2-binary
http://localhost:8000/home no issue working with database
Make django Migrations and migrate :
$ python manage.py migrate
$ python manage.py runserver
Models.py
To store data in database,we create migration.
C:\Users\Manibala\desktop\portfolio-project> python manage.py makemigrations
Now you can see auto generated code from models.py file in migrations/0001_initial.py -> this code is handled by Django. We should not touch it.if we see INSTALLED_APP in setting.py, from here all migration comes.
C:\Users\Manibala\desktop\portfolio-project> python manage.py migrate
It is running all those migrations and it’s getting postgres database.this means we are ready to save data in database.
Setting up admin panel:
http://localhost:8000/admin/login/?next=/admin/
Create user and password
Users\Manibala\desktop\portfolio-project> python manage.py createsuperuser
User:
Email: esc
Password:
User shows:
admin.py
from .models import Job
#Register your models here
admin.site.register(Job)
Uploaded Job now
Creating model objects via admin panel:
Click Add in Jobs
Upload image of code.jpg and save
Now you created 1 object in database. You can edit and save in database.
Now you can see image folder in portfolio project.
Models.py : we wrote the code to store images in images folder .
Pulling objects from the database :
We need to big all jobs in main page in localhost, not in admin link.
Views.py:
from .models import Job
def homepage(request): # Nick function
jobs = Job.objects # take job objects to html page
# send back html file
return render(request, 'jobs/home.html', {'jobs':jobs})#3rd is dictionary, Left side string
---------------------
home.html
<h1>All my jobs</h1>
{% for job in jobs.all %}
{{ job.summary }}
{% endfor %}
Bootstrap overview and installation: just change html and add js(b/w /footer and /body) and css library
https://getbootstrap.com/docs/4.4/examples/album/->rht clk->view page source-> copy->
Paste into home.html page-> save
Copy library
https://getbootstrap.com/->Get Started->copy CSS library-> paste into Bootstrap core CSS in home.html
Copy JS library all from bootstrap->delete all js script between </footer> and </body> & paste js library in home .html
Home.html
<h1>All my jobs</h1>
{% for job in jobs.all %}
{{ job.summary }}
{% endfor %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.6">
<title>Album example · Bootstrap</title>
<link rel="canonical" href="https://getbootstrap.com/docs/4.4/examples/album/">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<!-- Favicons -->
<link rel="apple-touch-icon" href="/docs/4.4/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/docs/4.4/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="icon" href="/docs/4.4/assets/img/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
<link rel="manifest" href="/docs/4.4/assets/img/favicons/manifest.json">
<link rel="mask-icon" href="/docs/4.4/assets/img/favicons/safari-pinned-tab.svg" color="#563d7c">
<link rel="icon" href="/docs/4.4/assets/img/favicons/favicon.ico">
<meta name="msapplication-config" content="/docs/4.4/assets/img/favicons/browserconfig.xml">
<meta name="theme-color" content="#563d7c">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="album.css" rel="stylesheet">
</head>
<body>
<header>
<div class="collapse bg-dark" id="navbarHeader">
<div class="container">
<div class="row">
<div class="col-sm-8 col-md-7 py-4">
<h4 class="text-white">About</h4>
<p class="text-muted">Add some information about the album below, the author, or any other background context. Make it a few sentences long so folks can pick up some informative tidbits. Then, link them off to some social networking sites or contact information.</p>
</div>
<div class="col-sm-4 offset-md-1 py-4">
<h4 class="text-white">Contact</h4>
<ul class="list-unstyled">
<li><a href="#" class="text-white">Follow on Twitter</a></li>
<li><a href="#" class="text-white">Like on Facebook</a></li>
<li><a href="#" class="text-white">Email me</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="navbar navbar-dark bg-dark shadow-sm">
<div class="container d-flex justify-content-between">
<a href="#" class="navbar-brand d-flex align-items-center">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" aria-hidden="true" class="mr-2" viewBox="0 0 24 24" focusable="false"><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/></svg>
<strong>Album</strong>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarHeader" aria-controls="navbarHeader" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</div>
</header>
<main role="main">
<section class="jumbotron text-center">
<div class="container">
<h1>Album example</h1>
<p class="lead text-muted">Something short and leading about the collection below—its contents, the creator, etc. Make it short and sweet, but not too short so folks don’t simply skip over it entirely.</p>
<p>
<a href="#" class="btn btn-primary my-2">Main call to action</a>
<a href="#" class="btn btn-secondary my-2">Secondary action</a>
</p>
</div>
</section>
<div class="album py-5 bg-light">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Edit</button>
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
<footer class="text-muted">
<div class="container">
<p class="float-right">
<a href="#">Back to top</a>
</p>
<p>Album example is © Bootstrap, but please download and customize it for yourself!</p>
<p>New to Bootstrap? <a href="https://getbootstrap.com/">Visit the homepage</a> or read our <a href="/docs/4.4/getting-started/introduction/">getting started guide</a>.</p>
</div>
</footer>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
Page layout and templates:
home.html
Change title
Remove header <header></header> , if you don’t want.
Change inside <h1></h1> header. Delete second button link <a></a>
Change 1st primary button link <a></a>
<a href="mailto:manibalasinha1@gmail.com" class="btn btn-primary my-2">Email Me</a>
This button fires up.
Delete <footer></footer>
leave the last column, delete above all column, you can delete <div class =”col-md 4”>
Adding static images:
jobs/static folder-> keep .jpg image
settings.py-> static files->
Where to store static images.
Settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
To access this static files , we need to know what url and root we need.
Urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url('admin/', admin.site.urls),
#url(r'^admin/', admin.site.urls),
url('', jobs.views.homepage, name='home'), #function nick and url home
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
We are going to provide STATIC_URL and STATIC_ROOT. This is essentially pulling those two things that we just set in our settings.py here, and now applying them to our URLs to say: these are things that we want to use when we are showing static files.
Collectstatic :
C:\Users\Manibala\desktop\portfolio-project> python manage.py collectstatic
It searches through entire Django project and sees if there’s any folders named static where it needs to add things into collective static folder. Saying I took 62 static files and copied them to specific place. Admin part of Django has a bunch of static files whenever you collect static, it’s take all those different pieces and put those into folder there.
Now you can see static folder inside portfolio-project folder that contains admin-css,fonts,img,js, mani.jpg.
home.html
<html>
{% load static %}
<head>
After button tag we write <img> tag
<img src="{% static 'mani.jpg' %}" height="300"></img>
See image now
-----------------
Bootstrap as a static asset:
https://getbootstrap.com/docs/4.4/getting-started/download/
Download Compile CSS and JS->you will get bootstrap-4.4.1-dist.zip file
https://jquery.com/download/->download the compressed,productionjQuery3.4.1 ->
Rht clk save link as->save on desktop jquery-3.4.1.min.js
https://popper.js.org/-> install v1->
popper.min.js-> rht clk save link as->save in portfolio-project/jobs/static on desktop
portfolio-project/jobs/static/popper.min.js, jQuery-3.4.1.min.js, css, js
In order to run, use command collectstatic
PS C:\Users\Manibala\desktop\portfolio-project> python manage.py collectstatic
Now you static files-> it is added some static files.
Now, Delete href =”” and make static to Bootstrap core CSS
Home.html
#Now grab local static CSS file
href="{% static 'css/bootstrap.min.css' %}"
Delete integrity and crossorigin
Change javascript file into static file at the bottom
Delete src.com,
Copy the name of jquery that we have in static folder
Paste into src="{% static 'jquery-3.4.1.min.js' %}"
Same thing for popper and bootstrap
<script src="{% static 'jquery-3.4.1.min.js' %}" ></script>
<script src="{% static 'popper.min.js' %}" ></script>
<script src="{% static 'js/bootstrap.min.js' %}" ></script>
Finishing touch in design:
Connecting URLs and templates:
Creating views:
Designing object detail views:
URL paths with parameters:
Comments
Post a Comment