【发布时间】:2022-01-12 05:31:39
【问题描述】:
views.py
def productslist(request):
products = Products.objects.all()
context = {'products':products}
return render(request,'productslist.html',context)
def productsform(request):
return render(request,'productscreate.html')
def productsupdate(request):
return render(request,'productsupdate.html')
def productsAdd(request):
if request.method == "POST":
data = request.data
product = Products()
product.title = request.POST["title"]
product.description = request.POST["description"]
product.image = request.FILES.get("image")
product = Products(title=data['title'], description=data['description'], image=data['image'])
product.save()
serializer = Productserialize(product)
return Response(serializer.data)
def products_list(request):
if (request.method == 'GET'):
product = Products.objects.all()
serializer = Productserialize(product,many=True)
return Response(serializer.data)
class ProductsDetailView(DetailView):
template_name = "productsdetail.html"
queryset = Products.objects.all()
context_object_name = 'products'
model = Products
serializer_class = Productserialize
@api_view(['PUT'])
def productupdate(request,pk):
data = request.data
prod = Products.objects.get(id=pk)
serializer = Productserialize(prod,data=data, many=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
detail.html
<form>
Title:- {{products.title }} <br><br>
Description:- {{ products.description}}<br><br>
{% if products.image %}
Image :- <img src="{{products.image.url}}" alt="image"><br><br>
{% endif %}
<button><a href="/update/{{ products.id}}">Edit</a></button>
</form>
serializer.py
class Productserialize(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(required=False, allow_blank=True, max_length=100)
description = serializers.CharField(required=False, allow_blank=True, max_length=100)
image = serializers.FileField()
def create(self, validated_data):
"""
Create and return a new `Snippet` instance, given the validated data.
"""
return Products.objects.create(**validated_data)
def update(self, instance, validated_data):
"""
Update and return an existing `Snippet` instance, given the validated data.
"""
instance.id = validated_data.get('id', instance.id)
instance.title = validated_data.get('title', instance.title)
instance.description = validated_data.get('description', instance.description)
instance.image = validated_data.get('image', instance.image)
instance.save()
return instance
update.html
<form method="post" action="/update/{{prod.id}}" enctype="multipart/form-data">
{% csrf_token %}
<table>
<tr>
<td>Title:<br>
<input type="text" name="title" id="title" value="{{ prod.title }}"></td>
<br>
</tr>
<tr>
<td>Description:<br>
<textarea name="description" id="description" value="{{ prod.description }}">Description</textarea></td>
<br>
</tr>
<tr>
{% if prod.image %}
<td>Image:<br>
<input type="file" name="image" id="image" value="{{prod.image.url}}"></td>
{% endif %}
<br>
</tr>
<tr>
<td><button type="submit" id="update">Update</button></td>
</tr>
</table>
</form>
urls.py
urlpatterns = [
path('', views.index, name="index"),
path('productlist', views.login, name="login"),
path('productslist', views.productslist, name='productslist'),
path('productscreate',views.productsform,name='productscreate'),
path('productsadd', views.productsAdd, name="productsadd"),
path('api/', views.products_list, name="products_list"),
path('<int:pk>', ProductsDetailView.as_view(), name='productsdetail'),
path('update', views.productsupdate, name="productsupdate"),
path('update/<str:pk>', views.productupdate, name="productupdate"),
]
当我尝试更新表单数据时没有保存在数据库中,也没有在 api 中显示 我已经给出了 html、views 和 urls.py 我需要用 html 表单更新表单,我想在 api 中显示数据 请帮我解决这个问题 提前致谢
【问题讨论】:
-
您是否遇到任何错误?
-
没有任何错误..数据没有保存到数据库并且没有显示在 api 中
-
您是否尝试过使用 pdb 来调试更新是否被正确调用?当您进行 API 调用时,服务器日志中存在什么?您是否尝试过 curl/postman 发出浏览器以外的请求?
-
没有试过邮递员,但更新不起作用数据没有保存
-
尝试从产品序列化程序的更新方法中删除“instance.id = valid_data.get('id', instance.id)”。
标签: django django-rest-framework