Skip to content
Advertisement

Django execute previous action on refresh

I have tried to add a book in to the database using an HTML form. After the submission, the page redirect to a page where all the books are listed .Then whenever I refresh the page , the data is became duplicated. How do I resolve this problem?

urls.py

from django.urls import path
from . import views
app_name='library'
urlpatterns =[
    path('', views.home, name='home'),
    path('book/',views.book,name='book'),
    path('book_details/<int:book_id>',views.book_details,name='book_details'),
    path('book_edit/<int:book_id>',views.book_edit,name='book_edit'),
    path('book_delete/<int:book_id>',views.book_delete,name='book_delete'),
    path('update/<int:book_id>',views.update,name='update'),
    path('author/',views.author_view,name='author_view'),
    path('addbook/',views.add_book,name='add_book'),
    path('books/',views.add_submit,name='add_submit'),
    
]

views.py

def add_submit(request): 
    if request.method =='POST':
        title=request.POST.get('t_title')
        print(title)
        author_name=request.POST.get('a_author')
        author, created=Author.objects.get_or_create(Name=author_name)
        summary=request.POST.get('s_summary')
        date=request.POST.get('d_date')
        book=Book(Title=title,Author=author,Summary=summary,Published_date=date)
        book.save()
        books=Book.objects.all()

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

Template file:

<form action="{% url 'library:add_submit'  %}" method="POST">
            {% csrf_token %}
        
            
            <div class="form-outline mb-4">
              <input type="text" id="bname" name="t_title" class="form-control" />
              <label class="form-label"   for="bname">Title</label>
            </div>
          
           
            <div class="form-outline mb-4">
              <input type="text" id="bauthor" name="a_author" class="form-control" />
              <label class="form-label"  for="bauthor">Author</label>
            </div>

            <div class="form-outline mb-4">
                <textarea  rows="5" cols="33" id="bsummary"  name="s_summary" class="form-control"></textarea>
                <label class="form-label" for="bsummary">Summary</label>
              </div>

              <div class="form-outline mb-4">
                <input type="date" placeholder="" id="pdate" name="d_date" class="form-control" />
                <label class="form-label" for="pdate">Published_Date</label>
              </div>
          
          
          
            <!-- Submit button -->
            <button type="submit" class="btn btn-primary btn-block">SUBMIT</button>
          </form>

Advertisement

Answer

This is most common problem, the thing is that after dealing with POST data you should always return an HttpResponseRedirect, the tip is not specific to Django, but it’s a good web practice in general so:

urls.py:

urlpatterns =[
    ...
    path("success/", views.success, name="success"
    
]

views.py:

def add_submit(request): 
    if request.method =='POST':
        title=request.POST.get('t_title')
        print(title)
        author_name=request.POST.get('a_author')
        author, created=Author.objects.get_or_create(Name=author_name)
        summary=request.POST.get('s_summary')
        date=request.POST.get('d_date')
        book=Book(Title=title,Author=author,Summary=summary,Published_date=date)
        book.save()
        return redirect("library:success")

    else:
        books=Book.objects.all()

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

def success(request):
    return render("success.html")

success.html

<h2> The form has been successfully submitted.</h2>

<a href="{% url 'library:add-submit' %}"> Go back to form</a>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement