我已经安装了 cookiecutter django 并且已经启动并运行了所有东西。我遇到了更新视图未更新模型的问题。它只是不断地将流重定向到自身。
梦想/models.py
class Dream(models.Model):
title = models.CharField(_('dream title'), max_length=60)
slug = models.SlugField(editable=False)
writeup = models.TextField(default="Boilerplate")
slug 字段是从对象保存时的标题创建的。
梦想/urls.py
url(
regex=r'^$',
view=views.DreamListView.as_view(),
name='list'
),
url(
regex=r'^(?P<slug>[\w.@+-]+)/$',
view=views.DreamDetailView.as_view(),
name='detail'
),
url(
regex=r'^(?P<slug>[\w.@+-]+)/update/$',
view=views.DreamUpdateView.as_view(),
name='update'
),
梦想/views.py
class DreamListView(ListView):
model = Dream
queryset = Dream.objects.all().filter(in_production=True)
# These next two lines tell the view to index lookups by slug
slug_field = 'slug'
slug_url_kwarg = 'slug'
class DreamDetailView(DetailView):
model = Dream
# These next two lines tell the view to index lookups by slug
slug_field = 'slug'
slug_url_kwarg = 'slug'
class DreamUpdateView(UpdateView):
model = Dream
fields =['title', 'writeup', ]
def get_success_url(self):
return reverse('dreams:detail', kwargs={'slug': self.get_object().slug})
slug_field = 'slug'
slug_url_kwarg = 'slug'
更新视图模板
{% extends "base.html" %}
{% load static %}{% load i18n %}
{% block title %}Edit Dream - {{ dream.title }}{% endblock %}
{% block content %}
<form action="" method=”post”>
{% csrf_token %}
{{ form.as_p }}
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Update</button>
</div>
</div>
</form>
{% endblock %}
我可以通过 localhost:8000//update 访问表单
当我更改一个值并点击更新时,表单将重新显示原始数据。它不会重定向到详细信息页面。我假设更新失败。有没有办法查看是什么导致它失败?
谢谢。