django-admin startproject jq
For our project we will change the settings.py to include static files.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'my_templates')],
'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',
],
},
},
]
Inside my_templates directory we kept our register.html file, this file is our main file where we will connect our Bootstrap and JQuery static files.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('jq.urls'))
]
So any call to home page ( http://127.0.0.1:8000/register ) will direct to urls.py inside jq. Our urls.py inside jq is here .
from django.urls import path,include
from . import views
urlpatterns = [
path('register',views.register,name='register'),
path('',views.blank,name='blank'),
]
This will call the function register inside views.py. Now let us check views.py.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def register(request):
return render(request, 'register.html')
def blank(request):
return HttpResponse('Welcome to plus2net')
Where is our register.html page ? We will add that but before that let us do some settings STATIC_URL = '/static/'
Here we are defining our STATIC_URL, this we will use to refer in our pages.
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
Here we have set our static URL page so same can be used for our templates. Now create one directory saying static. Run this command.
python manage.py collectstatic
Now we are ready to add static pages to our html pages. We have created register.html page inside my_templates directory. Now modify the code.
{% load static %}
<html>
<head>
<link rel='stylesheet' href="{% static 'css/my_style.css' %}" type='text/css'>
</head>
<body>
<h2 class=my_h2>I am from inside my_templates dir , register.html page</h2>
</body>
</html>
First we have loaded the static tag. This tag we have used to tell Django where is my files to link. Note the line linking the .css file. This file is stored inside static directory. Its path is here .
<link rel='stylesheet' href="/static/css/my_style.css" type='text/css'>
Now similarly we will keep our JavaScript files, image files in different directories inside static directory.
<script src="{% static 'js/jquery-3.4.1.min.js' %}"></script>
{% load static %}
<html>
<head>
<link rel='stylesheet' href="{% static 'css/my_style.css' %}" type='text/css'>
</head>
<body>
<h2 class=my_h2>I am from inside my_templates dir , register.html page</h2>
<input type=button value='Click me' id='b1'>
<div id=t1></div>
<script src="{% static 'js/jquery-3.4.1.min.js' %}"></script>
<script>
$(document).ready(function() {
///////////////////////
$("#b1").click(function(){
$("#t1").html('You clicked the button');
});
/////////////////////
});
</script>
</body>
</html>
<button type='button' class='btn btn-danger' id='b1'>Click Me</button>
You can learn more on bootstrap buttons here.
{% load static %}
<html>
<head>
<link rel='stylesheet' href="{% static 'css/my_style.css' %}" type='text/css'>
<link rel='stylesheet' href="{% static 'css/bootstrap.css' %}" type='text/css'>
</head>
<body>
<h2 class=my_h2>I am from inside my_templates dir , register.html page</h2>
<button type='button' class='btn btn-danger' id='b1'>Click Me</button>
<div id=t1></div>
<script src="{% static 'js/jquery-3.4.1.min.js' %}"></script>
<script src="{% static 'js/bootstrap.js' %}"></script>
<script>
$(document).ready(function() {
///////////////////////
$("#b1").click(function(){
$("#t1").html('You clicked the button');
});
/////////////////////
});
</script>
</body>
</html>
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.