为什么我的Django UpdateView没有看到我的模型数据(特别是id/pk)?
我正在研究一个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
回答:
望着意见和做多一点研究,我意识到我没有完全理解我想要做什么;最重要的是我没有模板。我已经解决了该问题如下:
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 %}
这解决了ŧ他发表了感谢大家的评论。
以上是 为什么我的Django UpdateView没有看到我的模型数据(特别是id/pk)? 的全部内容, 来源链接: utcz.com/qa/266854.html