0

Currently, I am using this code to render a page:

return render(request, 'appname/bundles_edit.html', context)

This works fine. However, I would like to send two integers to my path:

path('bundles/<int:bundle_template_number>/<int:bundle_id>/', views.bundles_edit, name='bundles_item'),

How would I insert those two integers in my return render statement?

Edit: Views Functions:

def bundles(request):
    context = {}
    if 'bundle_to_edit' in request.POST:
        bundle = request.POST['bundle_to_edit']
        instance_as_list = bundle.split(',')
        template_number = instance_as_list[0]
        template_id = instance_as_list[1]
        return render(request, 'contractor/bundles_edit.html' + '/' + template_number + '/' + template_id,, context)
    return render(request, 'contractor/bundles.html, context)

def bundles_edit(request):
    context= {}
    return render(request, 'contractor/bundles_edit.html', context)

I know the first return render is wrong, but I am not sure how to do it properly.

Edit 2: Updated code.

def bundles(request):
    if 'bundle_to_edit' in request.POST:
        bundle = request.POST['bundle_to_edit']
        instance_as_list = bundle.split(',')
        template_number = instance_as_list[0]
        bundle_id = instance_as_list[1]
        context['bundle_template_number'] = template_number
        context['bundle_id'] = bundle_id
        return render(request, 'contractor/bundles_edit.html', context)
    return render(request, 'contractor/bundles.html, context)


def bundles_edit(request, bundle_template_number, bundle_id):
    context= {}
    context['bundle_template_number'] = bundle_template_number
    context['bundle_id'] = bundle_id
    return render(request, 'contractor/bundles_edit.html', context)

urls.py:
path('bundles/<int:bundle_template_number>/<int:bundle_id>/', views.bundles_edit, name='bundles_item'),

Everything works at this point, it just leaves me with the browser URL as: /contractor/bundles/ instead of /contractor/bundles/1/8/

6
  • It's not clear what do you exactly mean. Do you need to pass this values to template? Do you see any errors? Can you show full view code? Commented Jan 23, 2020 at 14:14
  • Essentially, I want this to url to be created: website.com/appname/bundles/2/3. I just want to insert those two numbers. Here is pseudocode of what I want: return render(request, 'appname/bundles_edit.html'/2/3, context). If I do that, it says TemplateDoesNotExist at 'appname/bundles_edit.html/2/3. Commented Jan 23, 2020 at 14:17
  • Sure. I will add it to the question. Commented Jan 23, 2020 at 14:20
  • 1
    So from bundles view you want to redirect to bundles_edit view? Commented Jan 23, 2020 at 14:32
  • 1
    You seem to be confusing render() and redirect() and the template path with the url arguments. You certainly want to read the doc about urls and the reverse() function (docs.djangoproject.com/en/3.0/ref/urlresolvers/#reverse) Commented Jan 23, 2020 at 14:46

1 Answer 1

1

You can see that your path is good, like they said in the docs :

path('articles/<int:year>/<int:month>/', views.index)
def index(request, year, month):
    ...
    year = Years.objects.get(pk=year)
    month = Months.objects.get(pk=month)
    return render(request, 'yourtamplate.html', {'year':year, 'month':month})

Is that your idea?

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that is my idea. That works. Thanks! I notice it doesn't show up in the URL when I land on the final page. (in my browser URL). Is that just the way it is, or is there a way to make it show up in the URL? For instance if I was to copy and paste the url it gives me, it doesn't include the parameters.
Hmm, as I remember, it should be visible in the URL bar of your browser. Can you update the code, with your current code, so I can see it?
I took what @bruno-desthuilliers said and that fixed the issue on the URL showing up in the browser.
Great, I wanted to suggest the same! Glad that you managed it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.