使用cookiecutter django进行基本表单处理

时间:2016-04-23 13:30:24

标签: django-views cookiecutter-django

我已经安装了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

访问表单

当我更改某个值并点击“更新”时,将使用原始数据重新显示该表单。它不会重定向到详细信息页面。我认为更新失败了。有没有办法看看是什么让它失败了?

感谢。

1 个答案:

答案 0 :(得分:1)

我将表单剪切并粘贴到网页上。

帖子周围的引号不是正确的引号

<form action="" method=”post”>

叹息.......