Home > Blockchain >  Django execute previous action on refresh
Django execute previous action on refresh

Time:02-03

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 >
              <input type="text" id="bname" name="t_title"  />
              <label    for="bname">Title</label>
            </div>
          
           
            <div >
              <input type="text" id="bauthor" name="a_author"  />
              <label   for="bauthor">Author</label>
            </div>

            <div >
                <textarea  rows="5" cols="33" id="bsummary"  name="s_summary" ></textarea>
                <label  for="bsummary">Summary</label>
              </div>

              <div >
                <input type="date" placeholder="" id="pdate" name="d_date"  />
                <label  for="pdate">Published_Date</label>
              </div>
          
          
          
            <!-- Submit button -->
            <button type="submit" >SUBMIT</button>
          </form>

CodePudding user response:

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>
  • Related