我正在尝试使用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文件,我试图在其中添加“编辑”链接,该链接将重定向到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']
当我运行它并转到页面时,它显示错误“无反向匹配”
最佳答案
您忘记传递url参数pk
尝试以下操作:
<a href="{% url 'profiles:task-update' pk=datamain.id %}">Edit</a>
关于python - Django UpdateView不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41329314/