我正在开发一个Django项目,其中我有一个模型(订单),其中包含从客户收到的各种订单。我正在尝试使用UpdateView(CancelOrder)通过id更新其中一个Order字段(status)。我已经编写了UpdateView并修改了urls.py,但是当我尝试通过url更新订单的状态时,我收到错误:找不到与查询匹配的订单,尽管存储了多个订单。我的问题是,为什么UpdateView没有看到我的模型数据?我错过了什么吗?
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
答案 0 :(得分:0)
观察评论并做更多研究,我意识到我并没有完全理解我想要做的事情;最重要的是,我没有模板。我已经解决了以下问题:
views.py
class CancelOrder(UpdateView):
model = Order
fields = ('status',)
template_name = 'menu/order_form.html'
def get_success_url(self):
return reverse('confirm')
模板。我已经解决了以下问题:
urls.py
urlpatterns = patterns('',
...
url(r'^updateorder/(?P<pk>[0-9]+)/$', views.CancelOrder.as_view(), name='cancel')
)
urlpatterns = format_suffix_patterns(urlpatterns)
模板
{% extends "menu/header.html" %}
{% block content %}
<form method="POST" action="">{% csrf_token %}
<div id='helper'>{% include 'menu/includes/helper.html' %}<div>
<input id="submit" type="submit" value="Cancel Order">
</form>
<p>*Type "cancelled" to cancel your order</p>
{% endblock %}
辅助
{% for field in form %}
<div>
<div><span>{{field.errors}}</div></span>
<label>{{field.label_tag}}</label>
<div>{{field}}</div>
<div>
{% endfor %}
这解决了这个问题,感谢评论中的每个人。