django CreateView demands pk

201 views Asked by At

this is my CreateView:

class LampCreateView(CreateView):
    model = Lamp
    template_name = 'shotmaker/test_cam.html'
    success_url = '/shotmaker/'

and its url:

url(r'^camera/create/$', views.LampCreateView.as_view(), name='lamp_create'),

when I try to open the URL I get this:

ValueError at /shotmaker/camera/create/
invalid literal for int() with base 10: 'create'

when I pass pk into the url:

url(r'^camera/create/(?P<pk>[-\w]+)$', views.LampCreateView.as_view(), name='lamp_create'),

the view opens ok.

Why does it need a pk if this is a CreateView? The pk is not supposed to exist yet!

1

There are 1 answers

1
Alasdair On BEST ANSWER

If you look at the full traceback, you should be able to see that it is not the LampCreateView that is raising the exception.

It sounds like you have another url pattern above the lamp_create which matches /shotmaker/camera/create/ first. For example:

url(r'^camera/(?P<pk>[-\w]+)/$', views.OtherView.as_view(), name='other_view')
url(r'^camera/create/(?P<pk>[-\w]+)$', views.LampCreateView.as_view(), name='lamp_create'),

To solve the problem, move the other_view url below the lamp_create url.