Supercharge your development with unmatched features:
Access a full terminal environment, run Linux commands, and manage your project’s dependencies directly within the IDE.
Browse and interact with websites directly within the IDE. Supports real-time interaction with web content without leaving the workspace.
Manage your project files and directories effortlessly within the IDE. Create, edit, rename, move, and delete files—all in one place.
Experience seamless code editing with real-time syntax highlighting, tab support, and intelligent code suggestions for a smoother development workflow.
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It takes care of much of the hassle of web development, enabling developers to focus on writing their application instead of reinventing the wheel.
To set up Django, install it via pip. Ensure you have Python installed on your system.
pip install django
Verify the installation by running the following command:
django-admin --version
Create a new Django project with the following command:
django-admin startproject myproject
Navigate into the project directory and start the development server:
cd myproject
python manage.py runserver
Visit http://127.0.0.1:8000/
to see your project in action.
Models in Django define the structure of your database. You can create models by creating Python classes that inherit from django.db.models.Model
.
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
After defining the model, run the migration commands to create the corresponding table in the database:
python manage.py makemigrations
python manage.py migrate
Views in Django are functions that handle requests and return responses. They can render templates, return JSON, or redirect to other views.
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return HttpResponse("Hello, Django!")
Views can also be linked to templates for dynamic content rendering.
In Django, URLs are mapped to views through a URL configuration file called urls.py
. This file defines the URL patterns for the project or app.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
Include the app’s urls.py
in the main project’s urls.py
:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
Templates in Django allow you to generate HTML dynamically. Django uses the Jinja2 template engine by default.
# Example:
# In the view function:
def home(request):
return render(request, 'home.html')
# In the template (home.html):
Welcome to Django!
Forms in Django are used to handle user input and validate it. You can define forms using Django’s form classes.
from django import forms
class PostForm(forms.Form):
title = forms.CharField(max_length=100)
content = forms.CharField(widget=forms.Textarea)
# In the view:
def post_create(request):
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
# Process form data
pass
else:
form = PostForm()
return render(request, 'post_create.html', {'form': form})
Static files (CSS, JavaScript, images) are stored in a directory named static
. You can refer to them in your templates using the static
template tag.
{% load static %}
Django comes with a powerful admin interface that allows you to manage your database records. To use the admin interface, register your models in admin.py
.
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Then, create a superuser to access the admin interface:
python manage.py createsuperuser
Once the superuser is created, access the admin interface at http://127.0.0.1:8000/admin/
.