Django
Django
When we talk about server side and client side in the context of web
development, we are referring to the division of tasks between the
server and the client
Serverside
The server is responsible for processing requests, managing
the application logic, interacting with the database, and
generating dynamic content.
In a Django application, the server-side components include
the models, views, and the business logic implemented in
Python.
Server-side processing occurs on the web server before the
content is sent to the client
Client side
The client is the user's browser, which is responsible for rendering the content,
handling user interactions, and managing the presentation layer.
In a Django application, the client-side components include HTML, CSS which are
responsible for rendering the UI and handling user interactions.
<!DOCTYPE html>
Declares the document type and version of HTML. This declaration ensures that the browser
interprets the document correctly.
HTML Element:
<html lang="en">
The root element of an HTML document. The lang attribute specifies the language of the
document.
Head Element:
<head>
<!-- Meta information, links to stylesheets, and other head elements -->
</head>
Contains meta-information about the HTML document, such as character set, title,
and links to external resources
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Specifies the character set and sets the viewport for responsive design.
Title Element:
<title>Your Page Title</title>
Defines the title of the HTML document, which appears in the browser's title bar or
tab.
Body Element:
<body>
<!-- Content of the HTML document -->
</body>
Contains the content of the HTML document, including text, images, links, forms,
etc.
Headings:
<p>This is a paragraph.</p>
Represents a paragraph of text.
Lists:
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ol>
<li>Item 1</li>
<li>Item 2</li>
</ol>
Defines unordered (bulleted) and ordered (numbered) lists
Links:
Images:
Forms:
<div class="container">
<!-- Content inside a div -->
</div>
<div> is used for grouping and applying styles, while <span> is used for inline styling
Comments:
selector {
property: value;
}
To add CSS styles to an HTML document, you have a few different options. Here are three common
methods:
1. Inline Styles:
Inline styles are added directly to the HTML elements using the style attribute.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inline Styles Example</title>
</head>
<body>
<h1 style="color: blue; text-align: center;">Hello, World!</h1>
<p style="font-size: 16px; margin-top: 20px;">This is a paragraph with inline styles.</p>
</body>
</html>
2. Internal Styles:
Internal styles are defined within the <style> tag in the HTML document's <head>
section.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Internal Styles Example</title>
<style>
h1 {
color: blue;
text-align: center;
}
p{
font-size: 16px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph with internal styles.</p>
</body>
</html>
3. External Styles:
External styles are defined in a separate CSS file, and the HTML document links to
this file using the <link> tag.
styles.css:
/* styles.css */
h1 {
color: blue;
text-align: center;
}
p{
font-size: 16px;
margin-top: 20px;
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External Styles Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph with external styles.</p>
</body>
</html>
Using external styles is generally preferred for better organization and maintainability,
especially when working on larger projects.
Choose the method that best fits your needs and the scale of your project. Keep in
mind that good separation of concerns (HTML for structure, CSS for styling) is a best
practice for web development.
JAVA SCRIPT
JavaScript in <head>
function myFunction() {
document.getElementById("demo").innerHTML =
"Paragraph changed.";
}
<!DOCTYPE html>
<html>
<body>
<h2>External JavaScript</h2>
<script src="myScript.js"></script>
</body>
</html>
Bootstrap
Bootstrap is a popular open-source front-end framework that allows
developers to build responsive and visually appealing web pages quickly. It
includes a set of pre-designed components, such as navigation bars,
buttons, forms, and typography styles, making it easier to create a
consistent and professional-looking user interface.
Here are the key points about Bootstrap:
Installation:
You can include Bootstrap in your project by adding the Bootstrap CSS and
JavaScript files to your HTML document. You can either download Bootstrap
and host it locally or include it from a CDN (Content Delivery Network). For
example, including Bootstrap from a CDN in your HTML file:
<!-- Add this to the head of your HTML file -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></
script>
Grid System:
Bootstrap's grid system is responsive and allows you to create layouts that
adapt to different screen sizes. It is based on a 12-column grid system,
making it easy to create flexible and responsive designs.
Components:
Bootstrap provides a wide range of pre-styled components that you can
use in your project. These include navigation bars, buttons, forms,
cards, modals, carousels, and more. You can easily customize these
components to suit your design requirements.
Responsive Design:
Bootstrap is designed to be mobile-first and includes responsive CSS
classes that help in creating websites that look good on various devices,
from small mobile screens to large desktop monitors.
Documentation:
Bootstrap has extensive documentation that provides detailed
information on all its components, utilities, and features. The official
Bootstrap documentation is a valuable resource for learning and
reference
Utilities:
Bootstrap includes utility classes that can be applied directly to HTML
elements to achieve common styling or layout adjustments. For example,
classes like text-center, m-2 (margin), p-3 (padding), etc
Django
Django is a high-level web framework written in Python that
encourages rapid development and clean, pragmatic design. It
follows the Model-View-Controller (MVC) architectural pattern but
refers to it as the Model-View-Template (MVT) pattern. Django
provides a set of tools and conventions to help developers build web
applications quickly and efficiently.
Now that you have a Django project, you can run it, and see what it looks
like in a browser.
Navigate to the /myproject folder and execute this command in the
command prompt:
py manage.py runserver
Django Create App
What is an App?
An app is a web application that has a specific meaning in your project, like a home page, a contact form, or a
members database.
In this tutorial we will create an app that allows us to list and register members in a database.
But first, let's just create a simple Django app that displays "Hello World!".
Create App
I will name my app members.
Start by navigating to the selected location where you want to store the app, and run the command below.
If the server is still running, and you are not able to write commands, press [CTRL] [BREAK] to stop the server and
you should be back in the virtual environment.
myproject
manage.py
myproject/
members/
migrations/
__init__.py
Templates
Create a templates folder inside the (your appname)folder, and create a HTML file named
(your filename.html)
The file structure should be something like this:
myproject
myproject
manage.py
myproject/
appname/
templates/
Myfirst.html
Myfirst.html
If you want to specify the location of your templates in the settings.py file of your Django project, you can do so by
setting the TEMPLATES configuration. Django, by default, looks for templates in the "app_name/templates/"
directory. However, if you want to customize this behavior, follow these steps:
Locate the TEMPLATES configuration. It's a list of dictionaries, and one of the dictionaries contains the
'APP_DIRS': True setting, which means Django will look for templates in the "app_name/templates/" directory.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
# ...
},
]
If you want to add additional directories to search for templates, you can add them to the 'DIRS' list. For example, to
include a project-level templates directory, you can do:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # Add this line
'APP_DIRS': True,
# ...
},
]
Make sure to import the os module at the beginning of your settings.py file:
import os
Now, Django will look for templates in both the app-specific templates directory and the project-level templates
directory. This allows you to organize your templates in a way that suits your project structure.
Remember to run,
python manage.py runserver
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static’)]