Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)/$']

5,321 views
Skip to first unread message

anyi.lloyd

unread,
Mar 5, 2018, 6:11:53 PM3/5/18
to Django users
Am new to Django, please i need help here. Am trying to add a subject into a particular id using generic CreatView, i keeping getting errors
Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)/$']

models.py

from django.db import models
from datetime import date
from django.core.urlresolvers import reverse
from django.shortcuts import redirect


class Profile(models.Model):
firstName = models.CharField(max_length=30)
middleName = models.CharField(max_length=30)
lastName = models.CharField(max_length=30)
dob = models.DateField(blank=True, null=True)
stud_id = models.CharField(max_length=5000)
gender = models.CharField(max_length=10)
parentsName =models.CharField(max_length=30)
address = models.CharField(max_length=250)
lga = models.CharField(max_length=250)
Religion = models.CharField(max_length=50)
email = models.EmailField(max_length=70, null=True, blank=True, unique=True)
phone = models.CharField(max_length=11, unique=True)
photo = models.FileField()
Comment = models.CharField(max_length=250)

def get_absolute_url(self):
return reverse('student:detail', kwargs={'pk': self.pk})

def __str__(self):
return self.firstName

class Course(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)
subject = models.CharField(max_length=20)
score = models.CharField(max_length=20)
grade = models.CharField(max_length=20)

def get_absolute_url(self,):
return reverse('student:detail', kwargs={'pk': self.pk})

views.py
from django.views import generic
from django.views.generic import View
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy, reverse
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from .models import Profile, Course
from .forms import UserForm



#Index view
class IndexView(generic.ListView):
template_name = 'student/index.html'
context_object_name = 'all_profile'

def get_queryset(self):
return Profile.objects.all()

#Detail view
class DetailView(generic.DetailView):
model = Profile
template_name = 'student/detail.html'

#Add student section
class ProfileCreate(generic.CreateView):
model = Profile
fields = ['firstName', 'middleName', 'lastName', 'dob', 'gender', 'parentsName', 'address',
'lga', 'Religion', 'email', 'phone', 'photo']

#Add Subject section
class CourseCreate(CreateView):
model = Course
fields = ['subject', 'score', 'grade']


def get_form_kwargs(self):
kwargs = super(CourseCreate, self).get_form_kwargs()
kwargs['instance'] = Course(pk=self.kwargs['pk'])
return kwargs

urls.py

urlpatterns = [
#index url
url(r'^$', views.IndexView.as_view(), name='index'),

url(r'^signup/$', views.UserFormView.as_view(), name='signup'),
#details
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
#profile/add
url(r'^profile/add/$', views.ProfileCreate.as_view(), name='profile-add'),
#course/add
url(r'^(?P<pk>[0-9]+)/course_form/$', views.CourseCreate.as_view(), name='course_form'),
#profile/pk for update
url(r'^profile/(?P<pk>[0-9]+)/$', views.ProfileUpdate.as_view(), name='profile-update'),
#profile/pk for deleting
url(r'^profile/(?P<pk>[0-9]+)/delete/$', views.ProfileDelete.as_view(), name='profile-delete'),
]


template
{% extends 'student/base.html' %}
{% block title %}{% endblock %}

{% block body %}

<div class="container subject-container">
<div class="row">
<!-- profile photo-->
<div class="col-sm-4 col-md-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">{{profile.lastName}} {{profile.firstName}}</h1>
</div>
<div class="panel-body">
<a href="{% url 'student:detail' profile.id %}">
{% if profile.photo %}
<img src="{{ profile.photo.url }}" class="img-thumbnail" width="250px" hieght="100px">
{% else %}
<p class="small">No image to display</p>
{% endif %}
</a>
</div>
</div>

</div>

<!-- Subject Add-->
<ul class="nav nav-pills" style="margin-bottom: 10px;">
<li role="presentation"><a href="{% url 'student:detail' profile.id %}">View All</a></li>
<li role="presentation" class="active"><a href="{% url 'student:course_form' profile.id %}">Add New Song</a></li>
</ul>
<div class="col-sm-12 col-md-7">
<div class="panel panel-success">
<div class="panel-heading">
<h1 class="panel-title">Add Subject</h1>
</div>
<div class="panel-body">
<h3>Add Subject</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="hidden" name="profile" value="{{ profile }}">
{% include 'student/form-profile-temp.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>

</div>
</div>

</div>

</div>
</div>
{% endblock %}

Andy

unread,
Mar 7, 2018, 5:08:31 AM3/7/18
to Django users
If you actually are inside the profile create view, the profile probably does not have an id yet and thats why the url lookup is failing. The ID will be created on first save of the object.

anyi....@gmail.com

unread,
Mar 7, 2018, 2:15:39 PM3/7/18
to django...@googlegroups.com
The profile already have an id. Just wanted to add to an existing profile in the db with generic view.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/By5BEzS0ZJE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8ecfd645-1aa7-462b-90fb-9d78d0a0fc38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy Grabow

unread,
Mar 8, 2018, 8:10:12 AM3/8/18
to django...@googlegroups.com
Well, is it actually named profile inside the view? Rather than the default original? Check if the name is right and your profile is named profile in the template. 


For more options, visit https://groups.google.com/d/optout.
--

Mit besten Grüßen
Andy

anyi....@gmail.com

unread,
Mar 8, 2018, 1:48:44 PM3/8/18
to django...@googlegroups.com

Andy

unread,
Mar 8, 2018, 2:00:35 PM3/8/18
to Django users
which one of your calls to detail are failing?
<a href="{% url 'student:detail' profile.id %}">
or
reverse('student:detail', kwargs={'pk': self.pk})
?

if its the second one, try with args=[self.pk]

anyi....@gmail.com

unread,
Mar 8, 2018, 2:19:51 PM3/8/18
to django...@googlegroups.com
This <a href="{% url 'student:detail' profile.id %}">
But my views on the section of adding subject, please is it correct.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/61c6d7df-37f0-464e-9b7c-3f5c185a1033%40googlegroups.com.

anyi....@gmail.com

unread,
Mar 8, 2018, 2:19:54 PM3/8/18
to django...@googlegroups.com
Ok i will try out the second one you gave now. Thanks 

Sent from my HTC
Reply all
Reply to author
Forward
0 new messages