0
我正在研究一個Django項目,其中我有一個模型(訂單),其中包含從客戶收到的各種訂單。我試圖通過使用UpdateView(CancelOrder)通過id更新一個Order字段(狀態)。我寫了UpdateView並修改了urls.py,但是當我嘗試通過url更新訂單的狀態時,我得到錯誤:儘管存儲了多個訂單,但沒有找到與查詢匹配的訂單。我的問題是,爲什麼UpdateView沒有看到我的模型數據?我錯過了什麼嗎?爲什麼我的Django UpdateView沒有看到我的模型數據(特別是id/pk)?
Views.py
from django.shortcuts import render, redirect, HttpResponse
from django.core.exceptions import *
from django.views.generic.edit import UpdateView
from menu.models import Item, Order, Customer
from menu.forms import OrderForm, CustForm, UpdateForm
from .serializers import ItemSerializer
from rest_framework import generics
class CancelOrder(UpdateView):
model = Order
field = ('status',)
urls.py
from django.conf.urls import url, patterns, include
from . import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = patterns('',
url(r'^$', views.CreateOrder, name='CreateOrder'),
url(r'^vieworder/', views.ViewOrder, name='ViewOrder'),
url(r'^confirmation/', views.ConfirmOrder, name='ConfirmOrder'),
url(r'^api/$', views.ItemList.as_view()),
url(r'^api/(?P<pk>[0-9]+)/$', views.ItemDetail.as_view()),
url(r'^cancel/(?P<pk>[0-9]+)/$', views.CancelOrder.as_view(), name='CancelOrder'),
)
urlpatterns = format_suffix_patterns(urlpatterns)
型號
class Order(models.Model):
order_date = models.DateField(default=timezone.now)
delivery_date = models.DateField(default=timezone.now)
status = models.CharField(max_length=10, default='open')
ordered_by = models.CharField(max_length=50, null=True)
#user input
name = models.CharField(max_length=50)
email = models.EmailField()
phone = models.CharField(max_length=50)
shipping_info = models.CharField(max_length=50, null=True)
billing_info = models.CharField(max_length=50, null=True)
notes = models.CharField(max_length=150)
total = models.DecimalField(default=0, max_digits=5, decimal_places=2)
surcharge = models.DecimalField(default=0, max_digits=5,decimal_places=2)
def __str__(self):
return self.name
如何使用此視圖/網址?你在POST中傳遞了什麼? – AKS
這是需要做什麼?在我看到和閱讀的教程中,他們似乎只是設置了updateview,在urls.py中添加了一些行並與瀏覽器交互(手動鍵入/ cancel/id)。我確信有更多的實際方法可以做到這一點,但我是Django的新手。你會建議什麼? – Codarus
你讀過哪些教程?你能提供一個鏈接嗎? – AKS