重定向和反向操作如何与Django 2中的kwargs配合使用?

时间:2019-05-31 01:03:41

标签: django django-views

PaymentView函数可以正常工作,并且如果我不包含location变量,则可以正确地重定向到updateTransactions函数。

您能否解释一下为什么将位置变量添加为kwarg会破坏我的功能,以及如何解决此问题。

我收到错误pt 2出了问题

views.py

def PaymentView(request):

    if request.method == "POST":
        try:
            user_id = get_user_id(request)
            locations = 4
        except:
            messages.info(request, "Something went wrong pt 1")
        try:
            return redirect(reverse('memberships:update-transactions',
                                kwargs={
                                    'sub_id': user_id,
                                    'locations': locations,
                                }))

        except:
            messages.info(request, "Something went wrong pt 2")

    return render(request, "stuff/stuff.html")

def updateTransactions(request, sub_id, locations):

    user_membership = get_user_membership(request)

    user_membership.locations = locations
    user_membership.sub_id = sub_id
    user_membership.save()

    return redirect(reverse('courses:list'))

urls.py

from django.urls import path
from .views import PaymentView, updateTransactions

app_name = 'memberships'

urlpatterns = [
    path('payment/', PaymentView, name="payment"),
    path('update-transactions/<sub_id>', updateTransactions, name="update-transactions"),
]

0 个答案:

没有答案