Django-没有反向匹配错误-网址仅适用于静态值

时间:2019-02-26 12:14:37

标签: python django

我正在一个显示项目列表的Django网站上工作。您转到页面,看到一个项目,单击它,然后可以看到项目详细信息,并可以选择编辑页面。

对于每个单独的项目,我使用一个名为project.html的模板。然后在该页面上,我有一个指向edit_project.html

的链接。
<a href="{% url 'profile:edit_project' project.id %}">Edit Project</a>

每当我尝试加载项目页面进行查看时,都会出现此错误:

  

找不到带有参数'('',)'的'edit_project'的反向。 1个   尝试过的模式:['edit_project /(?P \ d +)/ $']

我检查了网址,它们看起来不错。因此,我尝试对我知道在数据库中的项目ID进行硬编码。例如,我打电话给:

   <a href="{% url 'profile:edit_project' 5%}">Edit Project</a>

而不是使用“ project.id”调用同一内容。当我这样做时,错误就消失了。

为什么这与硬编码值而不是project.id变量一起使用?

我还尝试删除数据库和所有迁移,以防万一。我还是遇到了同样的问题。

这是我用来设置所有内容的代码,以防万一。

project.html

{% extends "profile/base.html" %}

{% block content %}
    <p>
        <a href="{% url 'profile:edit_project' project.id %}">Edit Project</a>
    </p>
    <p>Status: {{ status }}</p>

{% endblock content %}

edit_project.html

{% block content %}
    <p><a href="{% url 'profile:project' project.id %}">{{ project }}</a></p>
    <p>Edit project:</p>

    <form action="{% url 'profile:edit_project' project.id %}" method='post'>
        {% csrf_token %}
        {{ form.as_p }}
        <button name="submit">Save Changes</button>
    </form>

{% endblock content %}

views.py

@login_required
def edit_project(request, project_id):
    """Edit an existing project"""
    project = Project.objects.get(id=project_id)
    #Make sure the project belongs to the owner
    if project.owner != request.user:
        return Http404
    if request.method != 'POST':
        #Initial request; pre-fill form with the current entry
        form = ProjectForm(instance=project)
    else:
        #POST data submitted; process data
        form = ProjectForm(instance=project, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('profile:project',args=[project.id]))
    context = {'project':project, 'form':form}
    return render(render, 'profile/edit_project.html', context)

(项目)forms.py

 class DateInput(forms.DateTimeInput):
            input_type = 'date'

    class ProjectForm(forms.ModelForm):
            class Meta:
                    model = Project
                    fields = ['name','description','status', 'project_type', 'start_date','end_date', 'due_date']
                    labels = {'name':'Name', 'description':'Description:', 'status':'Status', 'project_type':'Project Type',
                              'start_date':'Start Date', 'end_date':'End Date', 'due_date': 'Due Date',}
                    widgets ={'description':forms.Textarea(attrs={'cols':80}),'start_date':DateInput(),
                              'end_date':DateInput(),'due_date':DateInput()}

urls.py

 urlpatterns = [
        #Home page
        url('^$', views.index, name='index'),

        #Show all projects
        url('projects/$', views.projects, name='projects'),

        #Details for single project
        url('^projects/(?P<project_id>\d+)/$', views.project, name='project'),

        #Page for editing a single project
        url('^edit_project/(?P<project_id>\d+)/$', views.edit_project, name='edit_project'),

    ]

0 个答案:

没有答案