A quick guide to learn Django - Full Stack Development
Django -Full Stack quick tutorial

A quick guide to learn Django - Full Stack Development

Django is a high-level Python web framework that helps developers build web applications quickly and easily. It provides a lot of out-of-the-box functionality, such as URL routing, database integration, and templating, which makes it easy for developers to focus on writing application-specific code rather than worrying about the low-level details.

Lets start...

Installing Django

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

pip install Django         

Creating a Django Project

Now that we have Django installed, we can create our first Django project. We can do this by running the following command:

django-admin startproject myproject         

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

Running the Development Server

Before we can start building our application, we need to run the development server. We can do this by running the following command:

bash
cd myproject python manage.py runserver         

This will start the development server, and we should be able to see our application by navigating to https://localhost:8000/ in our web browser.

Creating a Django App

Now that we have our project set up and our development server running, we can create our first Django app. We can do this by running the following command:

python manage.py startapp myapp         

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

Creating a Model

In Django, models are used to define the structure of our database tables. We can create a model by defining a class in myapp/models.py. For example, let's create a simple model to represent a blog post:

python
from django.db import models class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() pub_date = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title         

This model defines a Post class that has three fields: title, content, and pub_date. The __str__ method is used to provide a human-readable representation of the model.

Creating a Migration

Now that we have defined our model, we need to create a migration. Migrations are used to update the database schema based on changes to our models. We can create a migration by running the following command:

python manage.py makemigrations         

This will create a new migration file in myapp/migrations/.

Running Migrations

Now that we have created our migration, we need to apply it to the database. We can do this by running the following command:

python manage.py migrate         

This will update the database schema based on the changes defined in our migration.

Creating a View

In Django, views are used to define the logic for handling HTTP requests. We can create a view by defining a function in myapp/views.py. For example, let's create a view to display a list of blog posts:

python
from django.shortcuts import render from .models import Post def post_list(request): posts = Post.objects.all() return render(request, 'myapp/post_list.html', {'posts': posts})         

This view defines a post_list function that retrieves all the Post objects from the database and passes them to a template for rendering.

Creating a Template

html
<!-- myapp/templates/myapp/post_list.html --> {% extends 'base.html' %} {% block content %} <h1>Blog Posts</h1> <ul> {% for post in posts %} <li>{{ post.title }} - {{ post.pub_date }}</li> {% endfor %} </ul> {% endblock %}         

This template extends a base template called base.html and defines a block called content. It then loops over the posts variable passed to the template and displays the title and publication date of each post.

Creating a URL

In Django, URLs are used to map HTTP requests to views. We can create a URL by defining a pattern in myapp/urls.py. For example, let's create a URL to display the list of blog posts:

python
from django.urls import path from .views import post_list urlpatterns = [ path('posts/', post_list, name='post_list'), ]         

This URL pattern maps the /posts/ URL to the post_list view.

Including URLs

Now that we have defined a URL pattern for our app, we need to include it in the project's URLs. We can do this by editing myproject/urls.py:

python
from django.urls import path, include urlpatterns = [ path('', include('myapp.urls')), ]         

This includes the myapp.urls module in the project's URL patterns.

Testing the App

Now that we have defined our model, view, template, and URL pattern, we can test our app. We can do this by running the development server and navigating to https://localhost:8000/posts/ in our web browser. We should see a list of all the blog posts in the database.

Congratulations! You have created a simple Django app from scratch.

Conclusion

In conclusion, Django is a powerful and versatile web framework that allows developers to quickly build web applications with minimal setup and configuration. In this tutorial, we covered the basics of creating a Django app, including creating a model, migrating the database, creating a view, creating a template, creating a URL pattern, including URLs in the project, and testing the app.

Of course, this is just the beginning of what you can do with Django. There are many more advanced features and concepts to explore, such as forms, authentication, middleware, and more. The official Django documentation is a great resource for learning more about these topics and becoming a proficient Django developer.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了