Lädt...


🔧 Beginner's Guide to Setting Up a Django Project


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Django is a powerful web framework for Python that allows you to build web applications quickly and efficiently. This guide will walk you through the process of setting up a Django project from scratch, perfect for beginners who want to get started with web development using Django.

Table of Contents

  1. Installing Python
  2. Setting Up a Virtual Environment
  3. Installing Django
  4. Creating a Django Project
  5. Creating a Django App
  6. Configuring the Database
  7. Creating Models
  8. Creating Views
  9. Creating Templates
  10. Configuring URLs
  11. Running the Development Server

1. Installing Python

Before we start with Django, make sure you have Python installed on your system. Different Django version requires different Python versions.

To check if Python is installed, open your terminal or command prompt and type:

python --version

If Python is not installed, download and install it from the official Python website (https://www.python.org/downloads/).

2. Setting Up a Virtual Environment

It's a good practice to create a virtual environment for each Django project. This keeps your project dependencies isolated from other projects.

To create a virtual environment:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following command:
python -m venv myenv

This creates a new virtual environment named "myenv".

To activate the virtual environment:

  • On Windows:
myenv\Scripts\activate
  • On macOS and Linux:
source myenv/bin/activate

You should see your prompt change to indicate that the virtual environment is active.

3. Installing Django

With your virtual environment activated, install Django using pip:

pip install django

4. Creating a Django Project

Now that Django is installed, let's create a new project:

django-admin startproject myproject

This creates a new directory called "myproject" with the basic Django project structure.

Navigate into the project directory:

cd myproject

5. Creating a Django App

Django projects are made up of one or more apps. Let's create an app for our project:

python manage.py startapp myapp

This creates a new directory called "myapp" with the basic app structure.

Open the myproject/settings.py file and add your new app to the INSTALLED_APPS list:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',  # Add this line
]

6. Configuring the Database

Django uses SQLite as its default database, which is fine for development. The database configuration is already set up in myproject/settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

To create the database tables, run:

python manage.py migrate

7. Creating Models

Models define the structure of your database. Let's create a simple model in myapp/models.py:

from django.db import models

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

    def __str__(self):
        return self.name

After creating or modifying models, run these commands to create and apply migrations:

python manage.py makemigrations
python manage.py migrate

8. Creating Views

Views handle the logic of your application. Create a simple view in myapp/views.py:

from django.shortcuts import render
from .models import Item

def item_list(request):
    items = Item.objects.all()
    return render(request, 'myapp/item_list.html', {'items': items})

9. Creating Templates

Templates are HTML files that define how your data is displayed. Create a new directory myapp/templates/myapp/ and add a file item_list.html:

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

10. Configuring URLs

To make your view accessible, you need to configure URLs. First, in myproject/urls.py:

from django.contrib import admin
from django.urls import path, include

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

Then, create a new file myapp/urls.py:

from django.urls import path
from . import views

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

11. Running the Development Server

You're now ready to run your Django project! Use this command:

python manage.py runserver

Visit http://127.0.0.1:8000/ in your web browser to see your Django application in action.

Congratulations! You've set up a basic Django project. This foundation will allow you to expand your project by adding more models, views, and templates as needed.

Follow me on my social media platforms for more updates and insights:

Remember to always activate your virtual environment before working on your project, and to run migrations whenever you make changes to your models.

...

🔧 Beginner's Guide to Setting Up a Django Project


📈 41.5 Punkte
🔧 Programmierung

🔧 A Beginner’s Guide to Django Web Framework: Django from Novice to Expert


📈 37.44 Punkte
🔧 Programmierung

🔧 Announcing my new Django package: django-admin-export! #packaging #python #django


📈 33.05 Punkte
🔧 Programmierung

🔧 A Beginner’s Guide to Setting Up a Project in Laravel


📈 30.49 Punkte
🔧 Programmierung

🔧 Host a Django project documentation autogenerated with Sphinx on Read the Docs -- Django specifics


📈 27.77 Punkte
🔧 Programmierung

🕵️ Low CVE-2021-21416: Django-registration project Django-registration


📈 27.77 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2019-10682: Django-nopassword project Django-nopassword


📈 27.77 Punkte
🕵️ Sicherheitslücken

🔧 Crafting Modern Web APIs with Django and Django REST Framework: A Comprehensive Guide


📈 27.5 Punkte
🔧 Programmierung

🔧 Complete Guide to the Django Services and Repositories Design Pattern with the Django REST Framework


📈 27.5 Punkte
🔧 Programmierung

🔧 Mastering Django's File & Folder Magic: A Beginner's Guide


📈 26.42 Punkte
🔧 Programmierung

🔧 A Beginner’s Guide to Django Web Framework: Creating Apps, Views, Configuring URLs and Templates


📈 26.42 Punkte
🔧 Programmierung

🔧 Introduction to Django: A Comprehensive Beginner's Guide to Backend Web Development


📈 26.42 Punkte
🔧 Programmierung

🔧 Building Your Django App: A Beginner's Guide (Part 2)


📈 26.42 Punkte
🔧 Programmierung

🔧 Build Your First Django App: Beginner's Guide


📈 26.42 Punkte
🔧 Programmierung

🔧 Mastering Django Now: A Comprehensive Guide from Beginner to Advanced


📈 26.42 Punkte
🔧 Programmierung

🔧 Setting up `pre-commit` in a Django project


📈 26.1 Punkte
🔧 Programmierung

🔧 Setting up your first Django Project


📈 26.1 Punkte
🔧 Programmierung

🔧 Comprehensive Guide to Setting Up Django


📈 25.83 Punkte
🔧 Programmierung

🔧 Setting Up an AWS EKS Cluster Using Terraform: A Beginner-Friendly Guide


📈 24.75 Punkte
🔧 Programmierung

🔧 Setting Up Node.js and Creating a Basic HTTP Server: A Beginner's Guide


📈 24.75 Punkte
🔧 Programmierung

🔧 Setting Up Node.js and Creating a Basic HTTP Server: A Beginner's Guide


📈 24.75 Punkte
🔧 Programmierung

🔧 Setting up Device Cloud: A Beginner’s Guide


📈 24.75 Punkte
🔧 Programmierung

🔧 Building Your Own Blockchain: A Beginner’s Guide to Setting Up and Using Cosmos


📈 24.75 Punkte
🔧 Programmierung

🔧 Building Your DevOps Playground: A Beginner's Guide to Setting Up Your Development Environment


📈 24.75 Punkte
🔧 Programmierung

🕵️ Django up to 1.8.17/1.9.12/1.10.6 django.views.static.serve Open Redirect


📈 22.03 Punkte
🕵️ Sicherheitslücken

🔧 Django-Sonar: The Essential Debugging Tool for Django


📈 22.03 Punkte
🔧 Programmierung

🕵️ Django bis 1.11.14/2.0.7 django.middleware.common.CommonMiddleware Open Redirect


📈 22.03 Punkte
🕵️ Sicherheitslücken

🔧 Django AllAuth Chapter 3 - Social login with Django AllAuth


📈 22.03 Punkte
🔧 Programmierung

🔧 FullStack Next.js & Django Authentication: Django REST, TypeScript, JWT, Wretch & Djoser


📈 22.03 Punkte
🔧 Programmierung

🕵️ Vuln: Django 'django.contrib.auth.views.login()' Function Open Redirection Vulnerability


📈 22.03 Punkte
🕵️ Sicherheitslücken

🔧 Build a Multivendor E-commerce Website using Django, React & Django Rest Framework


📈 22.03 Punkte
🔧 Programmierung

🔧 Django AllAuth Chapter 2 - How to install and configure Django AllAuth


📈 22.03 Punkte
🔧 Programmierung

matomo