django-admin startproject my_project # create project
cd my_project/
python3 manage.py startapp home # create app
python3 manage.py runserver # run server
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')) # All the urs from home.urls will be added to urlpatterns
]
- create
urls.py
in app
from django.contrib import admin
from django.urls import path
from home import views
urlpatterns = [
path('', views.index, name='home' ),
path('docs', views.docs, name='docs' ),
path('about', views.about, name='about' ),
path('services', views.services, name='services' ),
path('contact', views.contact, name='contact' ),
path('profile', views.profile, name='profile' ),
path('bsLearning', views.bsLearning, name='bsLearning' ),
path('LearningDTL', views.LearningDTL, name='bsLearning' ),
path('add', views.add, name='Adding' ),
]
from django.shortcuts import render, HttpResponse
# Create your views here.
def index(request):
# return HttpResponse("<center><h1>Home π </h1></center>")
# To send variables to the template, we use the render function
context = {
'Name' : 'Ayon Karmakar',
'age' : 21,
'hobbies' : ['Coding', 'Gaming', 'Sleeping', 'Eating', 'Watching Anime'],
# 'hobbies' : {'p' :'Coding'}
'profileImageUrl': 'https://avatars.githubusercontent.com/u/80549753?s=400&u=74659f0d3a599612e461950bd720e16345ebf4c8&v=4',
}
return render(request, 'index.html', context)
# return render(request, 'bootstrapComp.html')
def docs(request):
return HttpResponse("<center><h1>Docs π </h1></center>")
def about(request):
# return HttpResponse("<center><h1>About π
°οΈ </h1></center>")
return render(request, 'about.html')
def services(request):
return HttpResponse("<center><h1>Services π οΈ </h1></center>")
def contact(request):
# return HttpResponse("<center><h1>Contact π </h1></center>")
if request.method == "POST": # if form is rendered else it will not be rendered
name = request.POST.get('name')
email = request.POST.get('email')
phone = request.POST.get('phone')
desc = request.POST.get('desc')
date = request.POST.get('date')
return render(request, 'contact.html')
return render(request, 'contact.html')
def profile(request):
# return HttpResponse("<center><h1>Profile π </h1></center>")
profileLinks = {
'pL' : ['https://us.123rf.com/450wm/moremar/moremar1903/moremar190300013/moremar190300013.jpg?ver=6',
'https://media.istockphoto.com/id/1193146236/vector/portrait-of-a-businessman-avatar-of-a-young-man-for-social-network.jpg?s=170667a&w=0&k=20&c=tzuR2fBVV1ThyKl3p-8DRPDj1yD4ttC5_myLku6CAT8=',
'https://media.istockphoto.com/id/1190616457/vector/head-of-a-little-asian-girl-in-profile-the-face-of-a-child-on-the-side-portrait-avatar.jpg?s=612x612&w=0&k=20&c=P2MQJWGzhx86zrHnvjOL8jRc7IMtv7mu1AVYMHwyudw=',
'https://media.istockphoto.com/id/1190616551/vector/head-of-a-little-asian-boy-in-profile-the-face-of-a-child-on-the-side-portrait-avatar.jpg?s=170667a&w=0&k=20&c=g24hXtlRXkXPCEGEtWijmxSV1Xu3TbPWY6oTRLNGTEI='
],
'pL1' : 'https://media.istockphoto.com/id/1188460614/vector/portrait-of-a-young-beautiful-asian-fashion-woman-vector-flat-illustration-asian-cute-girl.jpg?s=612x612&w=0&k=20&c=1oOJoBKyyhS_VjqRGHoZ1p-zyfmnpAI7TYM_9y5DqzM=',
'pL2' : 'https://media.istockphoto.com/id/1193146236/vector/portrait-of-a-businessman-avatar-of-a-young-man-for-social-network.jpg?s=170667a&w=0&k=20&c=tzuR2fBVV1ThyKl3p-8DRPDj1yD4ttC5_myLku6CAT8=',
'pL3' : 'https://media.istockphoto.com/id/1190616457/vector/head-of-a-little-asian-girl-in-profile-the-face-of-a-child-on-the-side-portrait-avatar.jpg?s=612x612&w=0&k=20&c=P2MQJWGzhx86zrHnvjOL8jRc7IMtv7mu1AVYMHwyudw=',
'pL4' : 'https://media.istockphoto.com/id/1190616551/vector/head-of-a-little-asian-boy-in-profile-the-face-of-a-child-on-the-side-portrait-avatar.jpg?s=170667a&w=0&k=20&c=g24hXtlRXkXPCEGEtWijmxSV1Xu3TbPWY6oTRLNGTEI='
}
return render(request, 'profiles.html',profileLinks)
def bsLearning(request):
# return HttpResponse("<center><h1>Bootstrap Learning π </h1></center>")
return render(request, 'bootstrapComp.html')
def LearningDTL(request):
# return HttpResponse("<center><h1>Bootstrap Learning π </h1></center>")
return render(request, 'learningDTL.html')
def add(request):
n1 = request.POST['num1']
n2 = request.POST['num2']
res = int(n1) + int(n2)
# return HttpResponse("<center><h1>Bootstrap Learning π </h1></center>")
return render(request, 'add.html', {'sum': res})
Create static folder and add path
STATICFILES_DIRS = [
BASE_DIR / "static",
# '/var/www/static/',
]
Create template folder and add path
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / "template",], # Added manually the path of the template folder
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Past this in your urls.py to Change the view
admin.site.site_header = "Carton π Network Admin"
admin.site.site_title = "Carton π Network Admin Portal"
admin.site.index_title = "Welcome to Carton π Network Researcher Portal"
{% block title %}
{% endblock title %}
{% block body %}
{% endblock body %}
{% extends 'base.html' %}
{% block title %} Website {% endblock title %}
{% block body %}
This is body
{% endblock body %}
For Admin settings
python3 manage.py makemigrations # To track changes ( changes in schema)
python3 manage.py migrate # (django used some default tables for authentications
python3 manage.py createsuperuser
django install & vertualenv Link
{% extends 'base.html' %}
{% block title %}Contact{% endblock title %}
{% block content %}
<div class="container-fluid px-0 mb-3" >
<img src="https://source.unsplash.com/1000x180/?phone,contact" class="d-block w-100 mx-0" alt="...">
</div>
<div class="container mb-3 py-4">
<h1 class="text-center">Contact Us</h1>
<form method="post" action="/contact">
{% csrf_token %}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter your Name">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Your Email">
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="phone" class="form-control" id="phone" name="phone" placeholder="Enter Your Phone Number">
</div>
<div class="form-group">
<label for="desc">Tell me about what you want to contact me for...</label>
<textarea class="form-control" id="desc" rows="3" name="desc"></textarea>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
{% endblock content %}
def add(request):
n1 = request.POST['num1']
n2 = request.POST['num2']
res = int(n1) + int(n2)
# return HttpResponse("<center><h1>Bootstrap Learning π </h1></center>")
return render(request, 'add.html', {'sum': res})
model.py docs
from django.db import models
# Create your models here.
class Contact(models.Model):
name = models.CharField(max_length=122)
email = models.CharField(max_length=122)
phone = models.CharField(max_length=12)
desc = models.TextField()
date = models.DateField()
def __str__(self):
return self.name
- First register the model in admin.py
- app register in settings
- Now to save changes and make table in db migrate
python3 manage.py makemigrations
makemigrations
- create changes and store in a file
migrate
- apply the pending changes created by makemigrations
Need to make changes
from django.contrib import admin
# Register your models here.
from home.models import Contact # This is the line that imports the Contact model from home/models.py
admin.site.register(Contact) # This is the line that registers the Contact model with the admin site
Video to understand some connection ... Add app's Config settings.INSTALLED_APPS
python3 manage.py migrate
Making queries in Django link
Contact.objects.all()
Contact.objects.all()[0]
Contact.objects.all()[0].name
Contact.objects.filter(name="AYON KARMAKAR")
Change in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
python3 manage.py flush