2

I have installed cookiecutter django and have gotten everything up and running. I have encountered a problem where an update view is not updating the model. It just keeps redirecting the flow to itself.

dream/models.py

class Dream(models.Model):

    title = models.CharField(_('dream title'), max_length=60)
    slug = models.SlugField(editable=False)
    writeup = models.TextField(default="Boilerplate")

The slug field is created from the title on object save.

dreams/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'
),

dreams/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'

template of update view

{% 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 %}

I can access the form via localhost:8000//update

When I change a value and hit Update, the form is redisplayed with the original data. It does not redirect to the detail page. I assume that the update is failing. Is there a way to see what is making it fail?

Thanks.

  • I have been going over this all afternoon. It looks like it is the form action="". I tried putting {% url 'dreams:update' %} for action, but it generates an error. I think it is trying to figure out the slug pattern. –  Apr 23 '16 at 21:41
  • action="{%url dreams:update dream.slug %} generates the correct url in the form. But the data is not getting updated and the Details view is not getting called on success. Which leads me to think it is failing but there is no exception or error message. –  Apr 23 '16 at 22:08
  • Your regex in `urls.py` may have a problem with `[\w.@+-]`. In regular expressions, ".", "@", "+", and "-" all have special meanings. Typically you get better results with Django regex for slugs using `[\w\-]+`. – pydanny May 18 '16 at 03:39

1 Answers1

1

I cut and paste the form out of a web page.

The quotation marks around post are not the right quotation marks

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

Sigh.......