I
I
Ivan2020-01-21 21:12:34
Django
Ivan, 2020-01-21 21:12:34

How to deal with Reverse for 'add_task' with no arguments not found error in Django?

When clicking the Add Task button on the project.html page , an error occurs:
Reverse for 'add_task' with no arguments not found. 1 pattern(s) tried: ['my_projects/project(?P[^/]+)/add_task/$'] Request
Method: GET
Request URL: 127.0.0.1:8000/my_projects/project2/add_task
began to study!!! urls.py
file :

app_name = 'table'
urlpatterns = [
    path('', views.index, name = 'index'),
    path('my_projects/', views.my_projects, name = 'my_projects'),
    path('add_project/', views.add_project, name = 'add_project'),
    path('my_projects/project<project_id>/', views.project, name = 'project'),
    path('my_projects/project<project_id>/add_task/', views.add_task, 
        name = 'add_task'),
]

views.py file :
def project(request, project_id):
    project = Project.objects.get(id = project_id)
    statuses = project.status_set.order_by('id')
    tasks = []
    if statuses:
        for status in statuses:
            tasks += status.task_set.order_by('id')
    context = {'project': project,'statuses': statuses, 'tasks': tasks}
    return render(request, 'table/project.html', context)

def add_task(request, project_id):
    if request.method != 'POST':
        taskForm = TaskForm()
    else:
        taskForm = TaskForm(request.POST)
        if taskForm.is_valid():
            taskForm.save()
            #return HttpResponseRedirect(reverse('table:project', project_id = project.id))
    context = {'taskForm': taskForm}
    return render(request, 'table/add_task.html', context)

models.py file :
class Project(models.Model):
    name = models.CharField(max_length = 50)
    description = models.TextField()

    def __str__(self):
        return str(self.name)

class Status(models.Model):
    project = models.ForeignKey(Project, on_delete = models.CASCADE)
    title = models.CharField(max_length = 50)

    def __str__(self):
        return str(self.title)
        
    
class Task(models.Model):
    status = models.ForeignKey(Status, on_delete = models.CASCADE)
    title = models.CharField(max_length = 300)
    desctription = models.TextField()

    def __str__(self):
        if len(str(self.title)) < 50:
            return str(self.title)
        else:
            return str(self.title)[:50] + "..."

project.html file :
{% extends 'table/base.html' %}
{% block content %}
    <ul>
    <p>{{project}}</p>
        {% for status in statuses %}
            <p1>
                {{ status }}
                <ul>
                    {% for task in tasks %}
                        {% if task.status == status %}
                            <li>{{ task }}</li>
                        {% endif %}
                    {% endfor %}
                </ul>
            </p1>
        {% empty %}
            <li>Нет статусов для задач</li>
        {% endfor %}
    </ul>
    <a href="{% url 'table:add_task' project.id %}">Добавить новую задачу</a>
{% endblock content %}

add_task.html file :
{% extends 'table/base.html' %}

{% block content %}
    <p>Поставьте новую задачу:</p>
    <form action="{% url 'table:add_task' %}" method="POST">
        {% csrf_token %}
        {{ taskForm.as_p }}
        <button name="submit">Добавить задачу</button>
    </form> 
{% endblock content %}

Sorry for the loudness, I can not understand exactly where the error is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-01-21
@IvanPLebedev

You yourself indicated in urls.py that add_task has a project_id parameter, and where is it in add_task.html:?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question