2

I would like to pass kwargs to my view function through URL.

urls.py

urlpatterns = [

# ------------- set relations --------------------
url(r'^approve-form/$', views.approve_form,
{'content_type':None, 'form_id':None,}, name='approve-form'),]

views.py

def approve_form(request, content_type=None, form_id=None): return HttpResponse('Working')

Now I am using reverse_lazy function to call the url from on of the model instance

models.py

class FormStatus(models.Model):
    content_type = models.ForeignKey(ContentType)
    form_id = models.IntegerField(verbose_name='Form Ref ID')


    def __str__(self):
        return "{}".format(self.content_type)


    def get_approve_link(self):
        return  reverse_lazy("flow-control:approve-form", kwargs={'form_id':self.form_id,
                                                              'content_type':self.content_type})'

ERROR

Reverse for 'approve-form' with arguments '()' and keyword arguments '{'content_type': <ContentType: admin sanc model>, 'form_id': 12}' not found. 1 pattern(s) tried: ['flow-control/approve-form/$']

Is something wrong with the approach or is there any better approach for this ?

Thanks in advance.

PS: I tried the url documentation but couldn't figure it out.

kt14
  • 838
  • 15
  • 25
  • `flow-control` is `namespace` for the app(flow_control) in which `approve-form` is named url. So I don't want to removed the namespace from it. – kt14 Jun 09 '17 at 21:45
  • check with reverse function also , i'm sure you must have checked https://stackoverflow.com/questions/13202385/django-reverse-with-arguments-and-keyword-arguments-not-found – tom Jun 09 '17 at 22:03
  • @tom it seems that I need to specify mention them in url itself before passing them as kwargs. Thank you – kt14 Jun 09 '17 at 22:10
  • I have updated my answer for this please check and upvote it also if you find it useful , thanks – tom Jun 09 '17 at 22:11

1 Answers1

2

Change your url to and check if its worked or not-

   urlpatterns = [

   # ------------- set relations --------------------
  url(r'^approve-form/(?P<content_type>\w+)/(?P<form_id>\d+)/$', views.approve_form, name='approve-form'),]

views

 def approve_form(request, content_type=None, form_id=None):
     return  HttpResponse('Working')
tom
  • 3,720
  • 5
  • 26
  • 48