Home > OS >  Reverse for 'new_project' with arguments '('',)' not found. 1 pattern(
Reverse for 'new_project' with arguments '('',)' not found. 1 pattern(

Time:10-06

traceback picture

I have just learned to use python and Django for 2 weeks, and I'm trying to build a to-do list. I encountered an error and I might need some advice on it.

In the to-do list, there is a page for all projects, and when u click into a project there will be a to-do list. I tried to achieve add a new project function by these codes, but it returns the error as shown in the title, I've been looking into many answers online, most of the reason that caused the issue was because of the inappropriate introduction of URL in HTML file or when u specified more values than form in views.py and u forgot to place them behind url.

I still can't understand the error, I would really appreciate it if u could give me some hints on where I might get wrong. Thanks

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 %}

CodePudding user response:

Thanks, Marcel h and BrianD, I found the answer, it turned out that it is the comment that caused the issue, even though I have made it as a comment, it still caused the problem, my solution is to delete that comment.

CodePudding user response:

Template tags within html comments still get rendered by the template engine. To demonstrate:

from django import template

t = template.Template("""
<!-- {% url 'test' %} -->
""")

t.render(template.Context())

This will return:

NoReverseMatch: Reverse for 'test' not found. 'test' is not a valid view function or pattern name.

Even though the template tag url is enclosed with an html comment, it still raises an error if there is no url named test.

  • Related