簡體   English   中英

UpdateView 不保存數據,返回 url 中的 object 字段數據

[英]UpdateView Doesn't Save Data, returns object field data in url

無法找出 django 中的 UpdateView 在保存時沒有保存表單數據的原因。 相反,當它將我重定向回未編輯的 object 詳細視圖時,它將數據保存在 url 中。

我正在為 CreateView 和 UpdateView 共享相同的模板

這是我的 Views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from django.views import View

from .models import Blog
from .forms import BlogForm

from django.views.generic import (
  CreateView,
  ListView,
  DetailView,
  UpdateView,
  DeleteView
)
class BlogListView(ListView):
  template_name = 'blogtemplates/blogs.html'
  queryset = Blog.objects.all()
  model = Blog
  paginate_by = 4

  def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    # Add in a QuerySet of all the books
    context['object_list'] = Blog.objects.all()
    return context

class BlogDetailView(DetailView):
  model = Blog
  template_name = 'blogview.html'
  queryset = Blog.objects.all()

  def get_object(self):
    id_ = self.kwargs.get("pk")
    return get_object_or_404(Blog, id=id_)

  def get_context_data(self, **kwargs):

    context = super().get_context_data(**kwargs)
    # Add in a QuerySet of all the books
    context['object_list'] = Blog.objects.all()
    return context

class BlogCreateView(CreateView):
  template_name = 'blogpost.html'
  form_class = BlogForm
  queryset = Blog.objects.all()

  def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context
    context = super().get_context_data(**kwargs)
    # Add in a QuerySet of all the books
    context['object_list'] = Blog.objects.all()
    return context

class BlogUpdateView(UpdateView):
  template_name = 'blogpost.html'
  form_class = BlogForm

  def get_object(self):
    id_ = self.kwargs.get("pk")
    return get_object_or_404(Blog, id=id_)
  
  def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context
    context = super().get_context_data(**kwargs)
    # Add in a QuerySet of all the books
    context['object'] = self.get_object()
    context['object_list'] = Blog.objects.all()
    print(context['object'])
    return context

這是我的 urls.py(應用內 urls.py)

from django.urls import path

from .views import (
  BlogListView, 
  BlogCreateView,
  BlogDetailView, 
  BlogUpdateView, 
  #BlogDeleteView
  )
app_name = 'blogs'
urlpatterns = [
  path('', BlogListView.as_view(), name='blog-list'),
  path('<int:pk>/', BlogDetailView.as_view(), name="blog-detail"),
  path('post/', BlogCreateView.as_view(), name="blogs-post"),
  path('<int:pk>/update', BlogUpdateView.as_view(), name='blogs-update')
  #path('<int:pk>/delete', BlogDeleteView.as_view(), name="blog-delete"),
]

這是呈現 blogpost.html 的 HTML 模板

{% extends 'base.html' %}

{% block content %}
{% if request.user.username == 'Thorba' %}
  <div class="container-fluid bg-5 text-center">
  {% if "update" in request.get_full_path %}
    <form action="." method="PUT">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Save"/>
    </form>
  {% else %}
    <form action="." method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Save"/>
    </form>
  {% endif %}
 </div>
{% else %}
  <div class="container-fluid bg-5 text-center">
    <h1 class="box li">Permission Denied</h1>
  </div>
{% endif %}
{% endblock %}

這是我在 UpdateView 上點擊 Save 后收到的 GET 請求

HTTP GET /11/?csrfmiddlewaretoken=F4syRqsTW8X8JtPPSpzpu6qsCf7lryMOlGAnqHFelFt2XuzWCVQaeHEeoINOLnvR&title=And+Yet+%E2%80%A6+Another+Blog&description=POST+METHOD+CHECK&blog=After+a+LOT+of+research+about+UpdateView%2C+I+still+haven%27t+found+a+way+to+SAVE+THE+DATA.+The+url+is+routing+the+UpdateView+correctly+but+after+hitting+save+it+redirects+me+to+the+same%2C+previous%2C+unedited+object+First+Edit 200 [0.01, 192.168.29.226:51360]

在這里,獲取請求中的“第一次+編輯”是我試圖保存在 UpdateView 表單中的內容,但進入了 url 並且 object 沒有改變。

保存后如何保存對博客所做的編輯?

在您的表單中, method="PUT"的值無效,請參閱方法屬性文檔,因此它回退到method="GET" ,這就是為什么您在點擊保存后收到 GET 請求,並且UpdateView不保存任何數據時它正在發送GET請求。

要解決此問題,請將表單中的method值更改為POST

然后你必須改變形式的邏輯。 不要{% if "update" in request.get_full_path %}之類的東西,這似乎是一個可靠的 hack - 如果您更改urls.py中的路徑,這將停止工作。 或者,如果有人隨意將?update=foolyou放入 url 中,它也會行為不端。 而不是你應該依賴模板上下文中object的存在/存在,即。 像這樣的東西:

{% if object %} # existing object, do update

<form action="{% url "blogs-update" pk=object.pk %}" method="POST">

{% else %} # object not present, creating new one

<form action="." method="POST">

{%e endif %}



暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM