Отправка параметров из оператора возврата в путь в Django - PullRequest
0 голосов
/ 23 января 2020

В настоящее время я использую этот код для рендеринга страницы:

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

Это отлично работает. Тем не менее, я хотел бы отправить два целых числа на мой путь:

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

Как мне вставить эти два целых числа в мой оператор return render?

Edit: Views Функции:

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)

Я знаю, что первый обратный рендер неверен, но я не уверен, как это сделать правильно.

Редактировать 2: Обновлен код.

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'),

Все работает на этом Дело в том, что он оставляет меня с URL-адресом браузера как: / contractor / bundles / вместо / contractor / bundles / 1/8 /

1 Ответ

1 голос
/ 23 января 2020

Вы можете видеть, что ваш путь хорош, как они сказали в документах:

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})

Это ваша идея?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...