09 – HOW TO REDIRECT TO ANOTHER PAGE AFTER SAVING DATA IN DJANGO

Spread the love

I the last video, we have added data imputing functionality but data can be duplicated if you click on the save twice. To prevent that, we want to redirect to another page after clicking save.

Import render redirect to allow you the page to be redirected to another after saving the data.

from django.shortcuts import render, redirect

Add return redirect immediately after form.save()

if form.is_valid():
    form.save()
    return redirect('/list_items')

Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

13 Comments

    • Make sure you use either space bar or tab and maintain it for that code block. You can highlight the code block to see if the indentations are using the same marks (…. or —-). One of the marks is Spaces and the other Tabs.

  • Im having an error,

    File “C:\youtube\venv\src\stockmgmt\views.py”, line 29
    return redirect(‘/list_items’)
    TabError: inconsistent use of tabs and spaces in indentation

    my code is this:

    from django.shortcuts import render, redirect
    from .models import *
    from .forms import StockCreateForm

    # Create your views here.

    def home(request):
    title = ‘Welcome’
    context = {
    “title” : title
    }
    return render(request, “home.html”,context)

    #query ni siya
    def list_items(request):
    title = ‘The list of items’
    queryset = Stock.objects.all()
    context = {
    “title” : title,
    “queryset”: queryset
    }
    return render(request, “list_items.html”,context)

    def add_items(request):
    form = StockCreateForm(request.POST or None)
    if form.is_valid():
    form.save()
    return redirect(‘/list_items’)
    context = {
    “form”: form,
    “title”: “Add Item”,
    }
    return render(request, “add_items.html”, context)

  • its really kinda wierd , i solve it by pressing ctrl+alt and L and indent it again.

    its weirdd. cuz the indention at the begining is very similar to urs and still the same. Weird

  • its really kinda wierd , i solve it by pressing ctrl+alt and L and indent it again.

    its weirdd. cuz the indention at the begining is very similar to urs and still the same. Weird v

Leave a Reply

Your email address will not be published. Required fields are marked *