Zagorodniy Olexiy
Zagorodniy Olexiy

Reputation: 2212

How does the reverse function work in Django?

I have two models that look like this:

class Respond(models.Model):
        id = models.AutoField(primary_key=True, unique=True)

class Product(models.Model):
        responde = models.ForeignKey(Responde, null=True)
        name = models.CharField(max_length=255)
        description = models.CharField(max_length=10, blank=True, null=True)
        price = models.CharField(max_length=50, blank=True, null=True)

And I have the form that after submitting, send the data to remote server, then receive xml respond. I have a script that parse xml files or file-like objects. Than I save xml data to the database.

class MyView(View):

        template_name = 'myapp/form.html'

        def get(self, request, *args, **kwargs):
                my_form = MyForm(prefix='my_form')
                return render(request, self.template_name, { 'my_form' : my_form })

        def post(self, request, *args, **kwargs):
                my_form = MyForm(request.POST, prefix='my_form')
                if my_form.is_valid():
                ## cleaned data
                r = Respond.objects.create() 
                for xmldata in products_xml:
                   p = Product(
                      name=xmldata['ProductName'],
                      description=xmldata['ProductDescription'],
                      price=xmlData['ProductPrice'],
                   )
                   p.respond = r
                   p.save()
                r.product_set.all() 
        return HttpResponseRedirect(reverse(respond:'result', args=(r.id)))

After that I want to redirect to the page that show all saving data from respond id.

In my case have invalid syntax error with reverse function string. I read official documentation about it but i don't really understand how does work reverse function. My views.py:

def result(request):
        all_products = Products.objects.all()
        template = get_template("booking/results.html")
        context = Context({ 'all_products':all_products })
        html = template.render(context)
        return HttpResponse(html)

My urls.py looks like this:

urlpatterns = patterns('',
        url(r'^$', views.MyView.as_view()),
        url(r'^(?P<respond_id>[0-9]+)/result/$', name='result'),
)

Help me to understand how should look like reverse function with url configuration. Thank you.

Upvotes: 0

Views: 467

Answers (2)

Jahan Balasubramaniam
Jahan Balasubramaniam

Reputation: 360

Instead of this:

return HttpResponseRedirect(reverse(respond:'result', args=(r.id)))

Try this:

return HttpResponseRedirect(reverse('result', args=[r.id]))

Upvotes: 1

RodrigoDela
RodrigoDela

Reputation: 1736

First you are missing the actual function which should be called in your urlpatterns:

urlpatterns = patterns('',
        url(r'^$', views.MyView.as_view()),
        url(r'^(?P<respond_id>[0-9]+)/result/$', views.result, name='result'),
)

The parameters for the reverse function should be:

reverse('result', args=['your-arguments'])

It is described here.

So you start with the name of the view you would like to reverse and, if needed, you append args and kwargs.

Upvotes: 1

Related Questions