Python & Web-Framework Django & Make App without Use REST API

Introducing

Python is a high-level, interpreted programming language known for its readability, simplicity, and versatility. Created by Guido van Rossum and first released in 1991, Python emphasizes code readability and allows developers to write clear and concise code. Here are some key features and benefits of Python.

Key Features of Python

  1. Readability and Simplicity: Python's syntax is designed to be readable and straightforward, making it an excellent choice for beginners and experienced developers alike.
  2. Interpreted Language: Python is an interpreted language, which means that code is executed line by line, making debugging easier and quicker.
  3. Dynamically Typed: Python is dynamically typed, so you don't need to declare the data type of a variable explicitly.
  4. High-Level Language: Python abstracts many complex details of the computer system, allowing developers to focus on the logic of their programs.
  5. Extensive Standard Library: Python comes with a vast standard library that includes modules and packages for a wide range of applications, such as web development, data analysis, machine learning, and more.

Common Uses of Python

  1. Web Development: Frameworks like Django and Flask make it easy to build robust web applications.
  2. Data Analysis and Visualization: Libraries such as Pandas, NumPy, and Matplotlib are widely used for data manipulation, analysis, and visualization.
  3. Machine Learning and AI: Python is a leading language for machine learning and AI, with powerful libraries like TensorFlow, Keras, and sci-kit-learn.
  4. Automation and Scripting: Python is often used for writing scripts to automate repetitive tasks.

Getting Started with Python

  1. Installation: Download and install Python from python.org. Python 3 is the recommended version.
  2. IDEs and Editors: Popular integrated development environments (IDEs) for Python include PyCharm, Visual Studio Code, and Jupyter Notebook.
  3. Learning Resources: Numerous resources are available for learning Python, including the official Python documentation, online tutorials, and courses on platforms like Coursera, edX, and Codecademy.

Define Web-Framework

A web framework is a software framework designed to support the development of web applications, including web services, web resources, and web APIs. Web frameworks provide a structured way to build and deploy web applications by offering a set of tools, libraries, and components that simplify common web development tasks.

Key Features of Web Frameworks

  1. Routing: Web frameworks handle URL routing, mapping URLs to specific functions or controllers in the application.
  2. Templating Engines: These engines allow developers to generate HTML dynamically, separating the presentation layer from the business logic.
  3. Database Abstraction: Web frameworks often include Object-Relational Mapping (ORM) tools to interact with databases, simplifying database operations.
  4. Form Handling and Validation: Frameworks provide tools for handling user input through forms and validating the data.
  5. Security: Frameworks include built-in security features like protection against common web vulnerabilities (e.g., SQL injection, Cross-Site Scripting (XSS), Cross-Site Request Forgery (CSRF)).

Types of Web Frameworks

  1. Full-Stack Framework
  2. Micro-Framework
  3. Front-End Framework
  4. Back-End Framework

Benefits of Using a Web Framework

  1. Rapid Development: Frameworks provide pre-built components and tools that accelerate development.
  2. Maintainability: Structured code and standardized practices improve code readability and maintainability.
  3. Scalability: Frameworks often include features to help scale applications efficiently.

Introduced Django

Django is a high-level, open-source web framework written in Python, designed to promote rapid development and clean, pragmatic design. It was created by Adrian Holovaty and Simon Willison in 2003 and publicly released under a BSD license in 2005. Django's primary goal is to ease the creation of complex, database-driven websites.

Key Features of Django

  1. MTV Architecture: Django follows the Model-Template-View (MTV) architectural pattern, which is similar to the Model-View-Controller (MVC) pattern. It separates the data model, user interface, and application logic.
  2. Admin Interface: Django automatically generates an admin interface for your models, making it easy to manage data.
  3. ORM (Object-Relational Mapping): Django includes a powerful ORM that allows developers to interact with the database using Python code instead of SQL.
  4. URL Routing: Django uses a URL dispatcher to map URLs to views, making it easy to design complex URL schemes.
  5. Form Handling and Validation: Django provides tools for handling and validating forms, simplifying user input processing.

Define Rest API

A REST API (Representational State Transfer Application Programming Interface) is a set of rules and conventions for building and interacting with web services. RESTful APIs use HTTP requests to perform CRUD (Create, Read, Update, Delete) operations on resources represented in a web service. REST APIs are designed to be stateless, scalable, and easily maintainable, making them a popular choice for web development.

Key Concepts of REST API

  1. Resources: The primary entities that the API manages, are identified by unique URLs (Uniform Resource Locators). Resources can be any kind of object, data, or service that can be accessed by the client.
  2. HTTP Methods: RESTful APIs use standard HTTP methods to perform operations on resources.
    • GET: Retrieve a resource or a collection of resources.
    • POST: Create a new resource.
    • PUT: Update an existing resource or create a new resource if it does not exist.
    • DELETE: Remove a resource.
    • PATCH: Apply partial modifications to a resource.
  3. Statelessness: Each request from the client to the server must contain all the information needed to understand and process the request. The server does not store any client context between requests.
  4. Representation: Resources can be represented in various formats, such as JSON (JavaScript Object Notation), XML (eXtensible Markup Language), or HTML. JSON is the most commonly used format for REST APIs due to its simplicity and readability.
  5. Uniform Interface: REST APIs follow a consistent and uniform interface, making it easier for clients to interact with the API. This includes standardized URIs, HTTP methods, and status codes.

After this here, we will learn the Python web framework Django and create an app without using a REST API, you can follow these steps.

1. Set Up Your Environment

  • Ensure you have Python installed. Django supports Python 3.6 and above.
  • Create a virtual environment to manage your project dependencies.
  • Install Django

sh Code

python -m venv myenv
source myenv/bin/activate       # On Windows: myenv\Scripts\activate
pip install django

2. Start a New Django Project

Create a new Django project using the ‘Django-admin command.

sh Code

django-admin startproject myproject
cd myproject

3. Create a New Django App

Create a new Django app within your project. Let's call it ‘myapp’.

sh Code

python manage.py startapp myapp

4. Set Up Your App

Add your app to the ‘INSTALLED_APPS’ list in ‘myproject/settings.py’.

Python Code

INSTALLED_APPS = [
    # other installed apps
    'myapp',
]

5. Define Models

In ‘myapp/models.py’, define the models you need for your app.

Python Code

from django.db import models

class Item(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    def __str__(self):
        return self.name

Apply the migrations to create the necessary database tables.

sh Code

python manage.py makemigrations
python manage.py migrate

6. Create Admin Interface

Register your models in ‘myapp/admin.py’ to manage them through the Django admin interface.

Python Code

from django.contrib import admin
from .models import Item
admin.site.register(Item)

7. Define Views

Create views in‘myapp/views.py’to handle requests and return responses.

Python Code

from django.shortcuts import render
from .models import Item
def index(request):
    items = Item.objects.all()
    return render(request, 'index.html', {'items': items})

8. Set Up URLs

Define URL patterns in ‘myapp/urls.py’ and include them in the project's URL configuration.

Python Code

from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
]

Python Code

from django.contrib import admin
from django.urls import include, path
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

9. Create Templates
 

Html Code

<!DOCTYPE html>
<html>
<head>
    <title>My App</title>
</head>
<body>
    <h1>Items</h1>
    <ul>
        {% for item in items %}
        <li>{{ item.name }}: {{ item.description }}</li>
        {% endfor %}
    </ul>
</body>
</html>

10. Run the Development Server

Start the Django development server and view your app in the browser.

sh Code

python manage.py runserver

Visit ‘http://127.0.0.1:8000/’ in your browser to see your app in action.

Conclusion

These steps will help you get started with Django and create a basic web application without using a REST API. For a more comprehensive understanding, refer to the official Django documentation, which provides detailed information and tutorials.