【问题标题】:Reverse for 'new_project' with arguments '('',)' not found. 1 pattern(s) tried: ['new_project/$']未找到带有参数 '('',)' 的 'new_project' 的反向操作。尝试了 1 种模式:['new_project/$']
【发布时间】:2021-11-26 07:06:33
【问题描述】:

traceback picture

我刚刚学会使用 python 和 Django 2 周,我正在尝试建立一个待办事项列表。我遇到了一个错误,我可能需要一些建议。

待办事项列表中,有一个所有项目的页面,当你点击一个项目时,会有一个待办事项列表。我试图通过这些代码实现添加新项目的功能,但是返回如标题所示的错误,我在网上查了很多答案,导致问题的原因大部分是由于URL引入不当在 HTML 文件中,或者当您在 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 %}

【问题讨论】:

  • 能否请您提供错误的堆栈跟踪?
  • 似乎是因为todo_lists:new_project不接受参数,而是在这里传递了一个参数:{% url 'todo_lists:new_project' project.id %}
  • @marcelh 我在描述的第一行添加了一张追溯图片,干杯伙伴
  • @BrianD 那只是一条评论 Brian:) 会不会是其他问题?
  • @BrianD 你是对的,即使我把它作为评论,它仍然导致问题,我删除它后,错误消失了,干杯队友

标签: python django django-views django-templates django-urls


【解决方案1】:

谢谢,Marcel h 和 BrianD,我找到了答案,原来是评论导致了问题,即使我把它作为评论,它仍然导致问题,我的解决方案是删除那个评论。

【讨论】:

    【解决方案2】:

    html cmets 中的模板标签仍由模板引擎呈现。演示:

    from django import template
    
    t = template.Template("""
    <!-- {% url 'test' %} -->
    """)
    
    t.render(template.Context())
    

    这将返回:

    NoReverseMatch:未找到“测试”的反向。 'test' 不是有效的视图函数或模式名称。

    即使模板标签 url 包含在 html 注释中,如果没有名为 test 的 url,它仍然会引发错误。

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2020-12-28
      • 1970-01-01
      • 2022-01-17
      • 2020-10-28
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      相关资源
      最近更新 更多