Django UpdateView无法正常工作

时间:2016-12-26 09:50:11

标签: python django

我正在尝试使用django的UpdateView,但由于某种原因它不起作用。

(URL.py)
from django.conf.urls import url
from . import views
app_name='profiles'
urlpatterns = [
url(r'^$', views.IndexView.as_view(),name='index'),
url(r'^(?P<pk>[0-9]+)$',views.DetailView.as_view(),name='detail'),
url(r'^task/add/$',views.TaskCreate.as_view(),name='task-add'),
url(r'^task/(?P<pk>[0-9]+)$',views.TaskUpdate.as_view(), name='task-update'),
url(r'^task/(?P<pk>[0-9]+)/delete/$',views.TaskDelete.as_view(), name='task-delete'),]

下面是我的Html文件,其中我正在尝试添加&#34;编辑&#34;链接将重定向到UpdateView。

(Details.PY this already has the second url mapped)
{% extends 'profiles/base.html' %}
{% block body %}
<H3>The total efforts are {{datamain.efforts}} {{datamain.status}} hours.</H3>

<a href="{% url 'profiles:task-update' %}">Edit</a>
   {% endblock %}

以下是Views.py

class IndexView(generic.ListView):
template_name = 'profiles/index.html'
def get_queryset(self):
    return Datamain.objects.all()


class DetailView(generic.DetailView):
    model = Datamain
    template_name = 'profiles/details.html'

class TaskCreate(CreateView):
    model=Datamain
    fields=['main_task','date_time','efforts','status']

class TaskUpdate(UpdateView):
    model=Datamain
    fields=['main_task','date_time','efforts','status']

class TaskDelete(DeleteView):
    model=Datamain
    success_url = reverse_lazy('profiles:index')
    fields=['main_task','date_time','efforts','status']

当我运行它并转到页面时,它显示错误&#34;没有反向匹配&#34;

1 个答案:

答案 0 :(得分:3)

你忘了传递网址参数pk试试这个:

<a href="{% url 'profiles:task-update' pk=datamain.id %}">Edit</a>