Skip to main content

Django Blog CRUD (Step-by-Step)





๐Ÿš€ Django Blog CRUD (Step-by-Step)

1️⃣ Model (models.py)


๐Ÿ‘‰ Idi mana database structure
๐Ÿ‘‰ Title, content, image, date store avutayi


2️⃣ Forms (forms.py)




๐Ÿ‘‰ Easy ga form generate cheyadaniki use avutundi


3️⃣ Views (views.py)

from django.shortcuts import render, redirect, get_object_or_404
from .models import Blog
from .forms import BlogForm

# READ (List)
def blog_list(request):
    blogs = Blog.objects.all().order_by('-created_at')
    return render(request, 'blog_list.html', {'blogs': blogs})


# CREATE
def blog_create(request):
    form = BlogForm(request.POST or None, request.FILES or None)
    
    if form.is_valid():
        form.save()
        return redirect('blog_list')

    return render(request, 'blog_form.html', {'form': form})


# UPDATE
def blog_update(request, pk):
    blog = get_object_or_404(Blog, pk=pk)
    form = BlogForm(request.POST or None, request.FILES or None, instance=blog)

    if form.is_valid():
        form.save()
        return redirect('blog_list')

    return render(request, 'blog_form.html', {'form': form})


# DELETE
def blog_delete(request, pk):
    blog = get_object_or_404(Blog, pk=pk)

    if request.method == "POST":
        blog.delete()
        return redirect('blog_list')

    return render(request, 'blog_confirm_delete.html', {'blog': blog})


4️⃣ URLs (urls.py)



5️⃣ Templates

๐Ÿ“„ blog_list.html

<h1>All Blogs</h1>

<a href="{% url 'blog_create' %}">Create New Blog</a>

{% for blog in blogs %}
    <div>
        <h2>{{ blog.title }}</h2>
        <p>{{ blog.content|truncatewords:20 }}</p>

        {% if blog.image %}
            <img src="{{ blog.image.url }}" width="200">
        {% endif %}

        <br>
        <a href="{% url 'blog_update' blog.pk %}">Edit</a>
        <a href="{% url 'blog_delete' blog.pk %}">Delete</a>
    </div>
    <hr>
{% endfor %}


๐Ÿ“„ blog_form.html

<h1>Blog Form</h1>

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}

    <button type="submit">Save</button>
</form>


๐Ÿ“„ blog_confirm_delete.html

<h2>Are you sure you want to delete "{{ blog.title }}"?</h2>

<form method="POST">
    {% csrf_token %}
    <button type="submit">Yes, Delete</button>
</form>

<a href="{% url 'blog_list' %}">Cancel</a>


6️⃣ Media Settings (IMPORTANT)


# settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# main urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Comments

Popular posts from this blog

Django ante enti? (Beginner Friendly Telugu Guide)

  Introduction Meeru web development start cheyyali anukuntunnara? Leka backend development nerchukovali anukuntunnara? Appudu meeru tappakunda vinnadi oka peru ๐Ÿ‘‰ Django Ee article lo manam simple ga, clear ga ardham ayyela chuddam — Django ante enti, enduku use chestaru, ela start cheyyali.

Python Day 5 – If Else Conditions

  ๐Ÿ Python Day 5 – If Else Conditions ๐Ÿง  Introduction ๐Ÿ Python Day 5 – If Else Conditions Ippati varaku manam operators nerchukunnam. I roju manam decision making (If-Else) nerchukundam. ๐Ÿ‘‰ Program lo condition base chesi decisions tiskovadaniki use avutundi ๐Ÿ’ก Example: Age 18 kanna ekkuva unte → eligible Lekapothe → not eligible ๐Ÿ”น Basic If Condition age = 20 if age > 18 : print ( "Eligible" ) ๐Ÿ”น If-Else Condition age = 16 if age >= 18 : print ( "Eligible" ) else : print ( "Not Eligible" ) ๐Ÿ”น If-Elif-Else marks = 75 if marks >= 90 : print ( "Grade A" ) elif marks >= 60 : print ( "Grade B" ) else : print ( "Grade C" ) ๐Ÿงช Real Example (User Input) age = int ( input ( "Enter your age: " )) if age >= 18 : print ( "You can vote ✅" ) else : print ( "You cannot vote ❌" ) ⚠️ Important Points ๐Ÿ‘‰ Indentation (space) very import...

Django Templates & HTML Rendering (Complete Guide Telugu)

  Introduction Previous article lo manam Views & URLs chusam. Ipudu next important concept ๐Ÿ‘‰ Templates & HTML Rendering Django lo dynamic HTML pages ela create cheyyalo ee article lo nerchukundam ๐Ÿ”ฅ ๐Ÿง  Templates ante enti? ๐Ÿ‘‰ Template ante HTML file "T emplate ante HTML file matrame kaadu, Django lo dynamic content ni display cheyadaniki use chestam. Example ga, database lo unna blog posts ni HTML page lo chupinchadaniki templates use avutayi. " But special enti ante: ๐Ÿ‘‰ Dynamic data display cheyyachu (Python data → HTML lo show) ๐Ÿ“ Templates folder create cheyyadam Mee app lo templates ane folder create cheyandi: myapp/ templates/ index.html ⚙️ settings.py lo config cheyyali settings.py open cheyandi: TEMPLATES = [ { 'DIRS': [], ... }, ] ๐Ÿ‘‰ Usually app templates automatic ga detect chestundi ๐Ÿ’ป Simple HTML Template index.html : <!DOCTYPE html> <html> <head> <title>Django Page</title> ...