Django刷新时执行上一个动作

332nm8kg  于 2023-02-05  发布在  Go
关注(0)|答案(1)|浏览(89)

我试过用HTML表单添加一本书到数据库中。提交后,页面重定向到一个列出所有书籍的页面。然后每当我刷新页面时,数据就变成重复的。我该如何解决这个问题?
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})

模板文件:

<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>
w6lpcovy

w6lpcovy1#

这是最常见的问题,问题是在处理完POST数据后,你应该总是返回一个HttpResponseRedirect,这个技巧不是Django特有的,但它是一个很好的网络实践,所以:
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>

相关问题