Friday, October 23, 2020
Home > django > Redirect to detail view in django after creation | Django Blog

Redirect to detail view in django after creation | Django Blog

Redirect to detail view in django after creation :   If you are learning django, you must have encountered many problems. Redirect to detail view is one of the problem. I also faced same problem. I figured it out. Now i am going to tell you in this tutorial.

I have made django login tutorial, django signup tutorial.

In this tutorial i will post about function based list view, redirect to list view, redirect to detail view.

Redirect to detail view in django after creation

In this django tutorial, i am making student management system, first we will create models for student, then we will create views and urls. After that i will create form and then render everything in template.

I have made a project, named it school. then i created an app called demo.  I hope you know all these basics of django like how to create project, how to create django app. I am not going to tell you these. Lets start without wasting time.

models.py 

 
from django.db import models
from django.urls import reverse

# Create your models here.
class Student(models.Model):
    student_name = models.CharField(max_length=100)
    father_name = models.CharField(max_length=100)
    mother_name = models.CharField(max_length=100)



    def get_absolute_url(self):
        return reverse('student-detail', kwargs = {
            'id' : self.id
        })


I have created a model for student, this is just a simple model, not very long model.  Run migrations.

python manage.py makemigrations

python manage.py migrate

Now, go views.py and add these lines .

views.py 



from django.shortcuts import render, redirect
from .models import Student
from .forms import StudentForm

def student_list(request):
    stud = Student.objects.all()

    context = {
        'stud' : stud,
    }

    return render(request, 'demo/student_list.html', context)

def student_detail(request, id):
    stud = Student.objects.get(id=id)

    context = {
        'stud' : stud

    }

    return render(request, 'demo/student_detail.html', context)

def student_form(request):
    form = StudentForm()
    if request.method == 'POST':
        form = StudentForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save()
            return redirect('student-detail', id=instance.pk)
    context = {'form':form}
    return render(request, 'demo/student_form.html', context)

form.py 

 


from django import forms
from .models import Student


class StudentForm(forms.ModelForm):
    class Meta:
        model = Student
        fields = '__all__'

 

Add these to urls.py 


from django.urls import path

from .import views

urlpatterns = [
    path('student_form/', views.student_form, name = 'student-form'),
    path('student/', views.student_list, name = 'student-list'),
    path('student/<int:id>/', views.student_detail, name = 'student-detail'),
]

Inside demo app, create a folder named templates and inside templates create a folder named demo. This is django way.

Now create student_form.html 

Add these lines.


<form action=” “, method=”POST”>
{% csrf_token %}
{{ form }}
<input type=”submit” value=”submit”>
</form>
Now create another file inside demo/templates/demo
create student_detail.html

Student : {{ stud.student_name }}<br>
Mother : {{ stud.mother_name }}<br>
Father : {{ stud.father_name }} <br>
You must be wondering, what is stud in this code. well, go to views.py and see what you have written to get a single student.
stud = Student.objects.get(id=id)

Now testing time. If you know django, you must have tested these code earlier.  Now run server.

python manage.py runserver
open google chrome or any browser.
type :  http://127.0.0.1:8000/demo/student_form/
student form

After filling the form you will be redirect to detail view.
Redirect to detail view

How redirect to detail view happened ?

In view.py, in student_form function.

instance = form.save()

return redirect('student-detail', id=instance.pk)

When form is saved, an instance is created.  when  redirecting to detail view, i called the name of detail view and instance.pk

pk is primary key here. in your case its id.

 

How to redirect to list view ?

Redirecting to list view or any other page is very easy task. In student_form function.

delete instance.

 


form.save()
return redirect('student-list')

student-list is the name in urls.py, which redirect to student-list page.


 

Conclusion : Django redirect view.

If you love this article than share it, who are facing some difficulties. If you find any articles in this tutorial, you must comment, what is wrong in this article.

Thank you for reading.

 

 

Leave a Reply

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