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.

Am Dienstag, 6. März 2018 00:11:53 UTC+1 schrieb anyi.lloyd:
>
> 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 %}
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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.

Reply via email to