0

Can anyone help me with this issue? I saw all posts which are similar to my issue, but I can't fix this up. Error:

Reverse for 'commit_add' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['git/project/(?P<pk>[0-9]+)/add_commit/$']

views.py

class CommitCreate(CreateView):
    template_name = 'layout/project_commit_detail.html'
    model = Commit
    fields = ['user', 'project', 'branch', 'commit_title', 'commit_body']
    success_url = reverse_lazy('git_project:index')

html form

<div class="container-fluid">
<a href="#demo1" class="btn btn-info btn-block" data-toggle="collapse">Add New Commit</a>
<div id="demo1" class="collapse" >
    <form class="form-horizontal" action="{% url 'git_project:commit_add' project.id %}" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {% if user.is_authenticated %}
        <label for="user">Commit created by: "{{ user.username }}"</label><br>
        <input id="user" type="hidden" name="user" value="{{ user.id }}">
        <label for="project">Commit for project: "{{ project.proj_title }}"</label><br>
        <input id="project" type="hidden" name="project" value="{{ project.id }}">
        <label for="branch">Branch: </label>
        <select>
            {% for branch in all_branches %}
            <option id="branch" name="branch">{{branch.branch_name}}</option>
            {% endfor %}
        </select><br>
        <label for="commit_title">Commit Title: </label>
        <input id="commit_title" type="text" name="commit_title"><br>
        <textarea id="commit_body" name="commit_body" rows="5" cols="50" placeholder="Commit body..."></textarea><br>
        <button type="submit" class="btn btn-success">Commit</button>
        {% endif %}
    </form>
</div>

url.py

url(r'project/(?P<pk>[0-9]+)/add_commit/$', views.CommitCreate.as_view(), name='commit_add'),

model.py

class Commit(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, default=0)
    project = models.ForeignKey(Project, on_delete=models.CASCADE, default=0)
    branch = models.ForeignKey(Branch, on_delete=models.CASCADE, default=0)
    commit_title = models.CharField(max_length=64)
    commit_body = models.CharField(max_length=16384)
    commit_date = models.DateTimeField(default=timezone.now)

I don't know why is happening. Can anyone help me? I am very new in Django. Thanks! :)

Johny
  • 75
  • 1
  • 9
  • `project.id` is null, you don't show where it is you add that to the context though – Sayse Mar 13 '17 at 13:19
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Mar 13 '17 at 13:19
  • I think it is not null. I don't know, but when I click on Commit button in html form, it redirects me on this url: git/project/25/add_commit/, and when I write this id on html to check if it is ok, it returns me 25... – Johny Mar 13 '17 at 13:28
  • 1
    Where do you add `project` to the context data? – Sayse Mar 13 '17 at 13:33
  • "project" is a a column in Commit table, it is a Foreign key. And project is a value of exact project. When I click on add commit, project ID is equal to ID of clicked project. – Johny Mar 13 '17 at 13:43
  • 1
    Ok... where do you "click" the project? At template rendering time, there is no clicked project, so django doesn't have an id of an unclicked project, hence `project` is null – Sayse Mar 13 '17 at 13:45
  • In this html form, var user is equal to authenticated user id, var project is equal to project i clicked in all project form, and the other fields has to be filled in with new values. When I put type="text" instead of type="hidden", it returns me the right project. – Johny Mar 13 '17 at 13:51
  • Somewhere in this HTML template add this: `Project ID is: {{ project.id }}`. Does it say anything there? – nik_m Mar 13 '17 at 16:26
  • Yes, it returns the ID of project I clicked on before. It returns correct ID value for project. – Johny Mar 13 '17 at 18:25
  • I solved it, problem was in html form in select tag. This is correct code in html form: `` – Johny Mar 13 '17 at 22:41

0 Answers0