3

I am working on a django project. in the project I have a dynamic url as follows

app_name = 'test'

urlpatterns = [
    path('root', views.root, name='root'),
    path('output/<str:instance>', views.output_page, name='output_page'),
]

There exists two pages in the application. In the root page there exists a form which when submitted should redirect to the output_page. But because the output_page is a dynamic url, I am not able to redirect.

Here is my views file

def root(request):
    if request.method == 'POST':

        name = request.POST.get('name')
        job = request.POST.get('job')

        return redirect("test:output_page")

    return render(request, 'test/root.html')

def output_page(request, instance):

    record = Object.objects.all(Instance_id=instance)

    return render(request, 'test/output_page.html', {'record': record})

Here is the Model

class Object(models.Model):
    Name = models.CharField(max_length=200, null=True, blank=True)
    Job = models.CharField(max_length=200, default="")
    Instance_id = models.CharField(max_length=200)

When the redirect happens I want the url to be as follows

http://127.0.0.1:8000/output/test-001

where test-001 is the instance_id in the model.

The output_page should filter all the data in the model with instance_id test-001

Sashaank
  • 880
  • 2
  • 20
  • 54
  • Does this answer your question? [Django and urls.py: How do I HttpResponseRedirect via a named url?](https://stackoverflow.com/questions/1208802/django-and-urls-py-how-do-i-httpresponseredirect-via-a-named-url) – pygeek Sep 08 '20 at 12:42

3 Answers3

3

Solution

The direct solution to your question would be the following:

from django.urls import reverse
from django.shortcuts import get_object_or_404
...
instance = get_object_or_404(Object, name=name, job=job)
redirect(reverse('test:output_page', args=instance))

However, it would be worth investigating class based views. I recommend using django’s built in RedirectView for this purpose.

References

Django Reverse: https://docs.djangoproject.com/en/3.1/ref/urlresolvers/

Django RedirectView: https://docs.djangoproject.com/en/3.1/ref/class-based-views/base/#redirectview

pygeek
  • 7,356
  • 1
  • 20
  • 41
  • Thanks for the reply. I am now getting this error `output_page() missing 1 required positional argument: 'instance'` – Sashaank Sep 08 '20 at 12:22
  • You can pass args and kwargs to reverse function. I’ve updated my answer to reflect this, but it’s also noted in referenced documentation. – pygeek Sep 08 '20 at 12:26
  • Hi. I am now getting this error `Reverse for 'output_page' with no arguments not found. 1 pattern(s) tried: ['output/(?P[^/]+)$']` – Sashaank Sep 08 '20 at 12:38
  • You need to ensure instance exists and is passed in before redirecting. – pygeek Sep 08 '20 at 12:40
  • Hi. I solved the error. I had made a reference to the page in the template. Once I removed that, the error was solved. Thanks a lot – Sashaank Sep 08 '20 at 12:43
3

You can do like this

return redirect(reverse("test:ouput_page",kwargs={'instance':str(instance_id)}))

Adithya
  • 1,688
  • 1
  • 10
  • 18
  • Hi. I am now getting this error `Reverse for 'output_page' with no arguments not found. 1 pattern(s) tried: ['output/(?P[^/]+)$']` – Sashaank Sep 08 '20 at 12:38
  • `instance` is given as a string in the URL so you need to send `str(instance_id)` in `kwargs` – Adithya Sep 08 '20 at 12:41
  • Hi. I solved the error. I had made a reference to the page in the template. Once I removed that, the error was solved. Thanks a lot – Sashaank Sep 08 '20 at 12:43
-1
instance_model = "xyz"
return redirect('output_page', instance=str(instance_model))
(or)
return redirect('test:output_page', instance=str(instance_model))
Surya Pratap Rana
  • 423
  • 1
  • 4
  • 9