我的模板网址标记导致NoReverseMatch错误。但我使用的是基于类的视图,错误与使用get_context_data
函数有关。如果我注释掉get_context_data
函数,我就不会收到错误。
这是我得到的错误:
NoReverseMatch at / task-manager / update-project / e75eac16-711b-4fb7-9b08-7516cae8433f /
使用参数'('',)'和关键字参数'{}'找不到'update-project'的反转。尝试了1种模式:['task-manager / update-project /(?P [a-zA-Z0-9 _-] +)/ $']
然后: 模板渲染期间出错
在模板[projectdir] / [appdir] / templates / tasks
中/update-project.html, error at line 7
Reverse for 'update-project' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['task-manager/update-project/(?P<pk>[a-zA-Z0-9_-]+)/$']
这是update-project.html:
{% extends "tasks/base.html" %}
{% block content %}
<h2>Update a project</h2>
<div class="form">
<form action="{% url 'task_manager:update-project' project.id %}" method="POST">
{% csrf_token %}
{{ form }}
<BR><BR>
<input type="submit" value="Submit" />
</form>
</div>
....
我的应用程序的urlconf是tasks / urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
# Index
url(r'^$', views.TaskView.as_view(), name='index'),
# TASKS
# E.g., /task/3j243o-Ofjdsof-3123
url(r'^tasks/(?P<pk>[0-9a-zA-Z_-]+)/$', views.TaskDetailView.as_view(), name='task_detail'),
# Adding tasks
url(r'^add-task/$', views.TaskCreate.as_view(), name='add_task'),
# Update/view tasks
url(r'^view-task/(?P<pk>[0-9a-zA-Z_-]+)/$', views.TaskUpdate.as_view(), name='view_task'),
# PROJECTS
# E.g., /project/234jf0we-324skl-34j
url(r'^projects/(?P<pk>[a-zA-Z0-9_-]+)/$', views.ProjectDetailView.as_view(), name='project_detail'),
# Adding projects
url(r'^add-project/$', views.ProjectCreate.as_view(), name='add_project'),
# Updating/viewing Projects
url(r'^update-project/(?P<pk>[a-zA-Z0-9_-]+)/$', views.ProjectUpdate.as_view(), name='update-project')
]
views.py的相关部分是:
class ProjectUpdate(UpdateView):
"""
This will be used to view and update projects
"""
template_name = 'tasks/update-project.html'
model = Project
context_object_name = 'project'
fields = ['name','status', 'purpose', 'vision', 'big_steps', 'context', 'priority', 'due_date', 'related_project']
def get_context_data(self, **kwargs):
"""
This pulls in related tasks to display them as links.
"""
# Call base implementation first to get a context
context = super(ProjectUpdate, self).get_context_data(**kwargs)
# TO DO: Add in querysets of related tasks
context['related_tasks'] = Task.objects.all()
model.py的相关部分:
class Project(models.Model):
"""
Project is for any multi-step thing that needs to be done. Tasks
will be associated with it.
"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200)
purpose = models.TextField(blank=True)
"""What is my purpose with this task?"""
vision = models.TextField(blank=True)
"""Vision refers to what the task will look like when successfully
completed"""
big_steps = models.TextField(blank=True)
"""What are the big steps (not task-level steps) that need to be
completed? Maybe split this out into a new class, similar to tasks"""
status = models.CharField(max_length=30, choices=STATUSES, default='pending')
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
context = models.CharField(max_length=50, choices=CONTEXTS)
priority = models.CharField(max_length=50, choices=PRIORITIES)
due_date = models.DateTimeField(blank=True, null=True)
related_project = models.ForeignKey("self", blank=True, null=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('task_manager:project_detail', kwargs={'pk': self.pk})
奇怪的是,如果我在视图中注释掉get_context_data函数,它就可以正常工作。
答案 0 :(得分:0)
看起来您忘记了返回上下文。如果没有return语句,它将隐式返回None
。这会导致反向匹配,因为url标记获取None
而不是所需的项目ID。
def get_context_data(self, **kwargs):
"""
This pulls in related tasks to display them as links.
"""
# Call base implementation first to get a context
context = super(ProjectUpdate, self).get_context_data(**kwargs)
# TO DO: Add in querysets of related tasks
context['related_tasks'] = Task.objects.all()
return context