Django for newbeeez

muzammil85

muzu-85

Posted on March 18, 2023

Django for newbeeez

let's explore some django !!!!!!

this is a beginner friendly tutorial

so what is django?

actually,

Django is a powerful web framework that makes building web applications easy and efficient. It is written in Python, and it follows the Model-View-Controller (MVC) architectural pattern. In this tutorial, we will walk through the basics of Django and build a simple web application.

lets dive into it ->

Step 1: Installing Django

Before we start building our application, we need to install Django. We can do this by running the following command in our terminal:

pip install Django
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating a Django Project

Once Django is installed, we can create our first Django project by running the following command in our terminal:

django-admin startproject myproject
Enter fullscreen mode Exit fullscreen mode

This will create a new directory called myproject that contains the basic structure of a Django project.

Step 3: Creating a Django App

Next, we need to create a Django app within our project. We can do this by running the following command:

python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

This will create a new directory called myapp that contains the basic structure of a Django app.

Step 4: Creating a Model

In Django, a model is a representation of a database table. We can define our model in the models.py file within our app directory. Here's an example:

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    message = models.TextField()

Enter fullscreen mode Exit fullscreen mode

Sure, here's a beginner-friendly tutorial on Django.

Step 5: Creating a View

In Django, a view is a function that handles HTTP requests and returns an HTTP response. We can define our view in the views.py file within our app directory. Here's an example:

from django.shortcuts import render
from .models import MyModel

def index(request):
    if request.method == 'POST':
        name = request.POST['name']
        email = request.POST['email']
        message = request.POST['message']
        MyModel.objects.create(name=name, email=email, message=message)
    return render(request, 'index.html')

Enter fullscreen mode Exit fullscreen mode

This view defines a function called index that handles HTTP requests. If the request is a POST request, it creates a new record in the MyModel table with the data submitted in the form. Finally, it returns a rendered template called index.html.

Step 6: Creating a Template

In Django, a template is a file that defines the structure and layout of a web page. We can define our template in the templates directory within our app directory. Here's an example:

<!DOCTYPE html>
<html>
<head>
    <title>My App</title>
</head>
<body>
    <h1>My App</h1>
    <form method="post">
        {% csrf_token %}
        <label for="name">Name:</label>
        <input type="text" id="name" name="name"><br><br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email"><br><br>
        <label for="message">Message:</label>
        <textarea id="message" name="message"></textarea><br><br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

This template defines an HTML form that allows users to submit their name, email, and message.

Step 7: Connecting Everything Together

Finally, we need to connect everything together. We can do this by defining our URLs in the urls.py file within our app directory. Here's an example:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

Enter fullscreen mode Exit fullscreen mode

This URL pattern maps the root URL ('') to the index view defined in views.py.

Next, we need to include our app's URLs in the project's URLs. We can do this by adding the following line to the urls.py file within our project directory:

path('', include('myapp.urls')),
Enter fullscreen mode Exit fullscreen mode

This line includes the URLs defined in myapp.urls within the project's URLs.

Step 8: Running the Server

Now that we have everything set up, we can run the server by running the following command in our terminal:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

This will start the server on http://127.0.0.1:8000/.

Step 9: Testing the Application

Open your browser and go to http://127.0.0.1:8000/. You should see the form we created in the template. Fill out the form and submit it. If everything is working correctly, you should see a new record in the MyModel table.

Conclusion

In this tutorial, we walked through the basics of Django and built a simple web application. We learned how to create a project, create an app, define a model, define a view, create a template, connect everything together with URLs, and run the server. With this foundation, you can continue to explore Django and build more complex web applications.

πŸ’– πŸ’ͺ πŸ™… 🚩
muzammil85
muzu-85

Posted on March 18, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related