反向 'new_project',未找到参数 '('',)'。尝试了 1 种模式:['new_project/$']
Reverse for 'new_project' with arguments '('',)' not found. 1 pattern(s) tried: ['new_project/$']
traceback picture
我刚刚学会使用 python 和 Django 2 周,我正在尝试建立一个待办事项列表。我遇到了一个错误,我可能需要一些建议。
在待办列表中,有一个所有项目的页面,当你点击进入一个项目时,会有一个待办列表。我试图通过这些代码实现添加新项目功能,但是出现returns如题所示的错误,网上查了很多答案,大部分是因为在 HTML 文件中不恰当地引入了 URL 或者当你在 views.py 中指定了比表格更多的值而你忘记将它们放在 url.
后面
我仍然无法理解该错误,如果您能给我一些提示,指出我可能出错的地方,我将不胜感激。谢谢
urls.py
app_name = 'todo_lists'
urlpatterns = [
# homepage
path('', views.index, name='index'),
# page that shows all projects
path('projects/', views.projects, name='projects'),
# details of a project
path('projects/<int:project_id>/', views.project, name='project'),
# add a new project
path('new_project/', views.new_project, name='new_project'),
views.py
def new_project(request):
# add new project
if request.method != 'POST':
# if not submit then create a new form for user
form = ProjectForm()
else:
form = ProjectForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('todo_lists:projects'))
# team', 'name', 'due_date', 'project_code', 'details
context = {'form': form}
return render(request, 'todo_lists/new_project.html', context)
models.py
class Project(models.Model):
name = models.CharField(max_length=20)
create_date = models.DateTimeField(auto_now_add=True)
due_date = models.DateTimeField(default=datetime.datetime.now)
project_code = models.CharField(max_length=20)
details = models.TextField()
team = models.CharField(max_length=200)
def __str__(self):
return self.details
forms.py
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ['team', 'name', 'due_date', 'project_code', 'details']
widgets = {
'start_date': forms.SelectDateWidget(),
'due_date': forms.SelectDateWidget(),
}
new_project.html
<!doctype html>
{% extends "todo_lists/base.html" %}
{% block content %}
<p>Add a new project:</p>
<form action="{% url 'todo_lists:new_project' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add project</button>
</form>
<!-- <a href="{% url 'todo_lists:new_project' project.id %}">Add a new project:</a>
-->
{% endblock content %}
projects.html
<!doctype html>
{% extends "todo_lists/base.html" %}
{% block content %}
<p>Projects</p>
<a href="{% url 'todo_lists:new_project' %}">Add new project</a>
<ul>
{% for project in projects %}
<li>
<a href="{% url 'todo_lists:project' project.id %}">{{ project.id }}</a>
<!-- <a href="{% url 'todo_lists:project' project.id %}">{{ project }}</a> -->
<p>Name: {{ project.name }}</p>
<p>Due date: {{ project.due_date }}</p>
<p>project_code: {{ project.project_code }}</p>
<p>details: {{ project.details }}</p>
<p>team: {{ project.team }}</p>
</li>
{% empty %}
<li>No project has been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
谢谢 Marcel h 和 BrianD,我找到了答案,原来是评论导致了问题,即使我已经把它作为评论,它仍然导致了问题,我的解决方案是删除该评论。
html 评论中的模板标签仍由模板引擎呈现。演示:
from django import template
t = template.Template("""
<!-- {% url 'test' %} -->
""")
t.render(template.Context())
这将 return:
NoReverseMatch: Reverse for 'test' not found. 'test' is not a valid view function or pattern name.
即使模板标签 url
包含在 html 注释中,如果没有名为 test
的 url,它仍然会引发错误。
traceback picture
我刚刚学会使用 python 和 Django 2 周,我正在尝试建立一个待办事项列表。我遇到了一个错误,我可能需要一些建议。
在待办列表中,有一个所有项目的页面,当你点击进入一个项目时,会有一个待办列表。我试图通过这些代码实现添加新项目功能,但是出现returns如题所示的错误,网上查了很多答案,大部分是因为在 HTML 文件中不恰当地引入了 URL 或者当你在 views.py 中指定了比表格更多的值而你忘记将它们放在 url.
后面我仍然无法理解该错误,如果您能给我一些提示,指出我可能出错的地方,我将不胜感激。谢谢
urls.py
app_name = 'todo_lists'
urlpatterns = [
# homepage
path('', views.index, name='index'),
# page that shows all projects
path('projects/', views.projects, name='projects'),
# details of a project
path('projects/<int:project_id>/', views.project, name='project'),
# add a new project
path('new_project/', views.new_project, name='new_project'),
views.py
def new_project(request):
# add new project
if request.method != 'POST':
# if not submit then create a new form for user
form = ProjectForm()
else:
form = ProjectForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('todo_lists:projects'))
# team', 'name', 'due_date', 'project_code', 'details
context = {'form': form}
return render(request, 'todo_lists/new_project.html', context)
models.py
class Project(models.Model):
name = models.CharField(max_length=20)
create_date = models.DateTimeField(auto_now_add=True)
due_date = models.DateTimeField(default=datetime.datetime.now)
project_code = models.CharField(max_length=20)
details = models.TextField()
team = models.CharField(max_length=200)
def __str__(self):
return self.details
forms.py
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ['team', 'name', 'due_date', 'project_code', 'details']
widgets = {
'start_date': forms.SelectDateWidget(),
'due_date': forms.SelectDateWidget(),
}
new_project.html
<!doctype html>
{% extends "todo_lists/base.html" %}
{% block content %}
<p>Add a new project:</p>
<form action="{% url 'todo_lists:new_project' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add project</button>
</form>
<!-- <a href="{% url 'todo_lists:new_project' project.id %}">Add a new project:</a>
-->
{% endblock content %}
projects.html
<!doctype html>
{% extends "todo_lists/base.html" %}
{% block content %}
<p>Projects</p>
<a href="{% url 'todo_lists:new_project' %}">Add new project</a>
<ul>
{% for project in projects %}
<li>
<a href="{% url 'todo_lists:project' project.id %}">{{ project.id }}</a>
<!-- <a href="{% url 'todo_lists:project' project.id %}">{{ project }}</a> -->
<p>Name: {{ project.name }}</p>
<p>Due date: {{ project.due_date }}</p>
<p>project_code: {{ project.project_code }}</p>
<p>details: {{ project.details }}</p>
<p>team: {{ project.team }}</p>
</li>
{% empty %}
<li>No project has been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
谢谢 Marcel h 和 BrianD,我找到了答案,原来是评论导致了问题,即使我已经把它作为评论,它仍然导致了问题,我的解决方案是删除该评论。
html 评论中的模板标签仍由模板引擎呈现。演示:
from django import template
t = template.Template("""
<!-- {% url 'test' %} -->
""")
t.render(template.Context())
这将 return:
NoReverseMatch: Reverse for 'test' not found. 'test' is not a valid view function or pattern name.
即使模板标签 url
包含在 html 注释中,如果没有名为 test
的 url,它仍然会引发错误。