简体   繁体   English

在 django 中对 url1 执行任务后,如何重定向到 url2?

[英]How do I redirect to url2 after performing a task on url1 in django?

I have a edit-scholarship.html in which you can search for a scholarship by passing name and type and then select that scholarship and edit it in update-scholarship.html by passing scholarship id from the url.我有一个edit-scholarship.html ,您可以在其中通过传递名称和类型来搜索奖学金,然后选择该奖学金并通过从 url 传递奖学金 IDupdate-scholarship.html 中对其进行编辑。 Now after updating the scholarship, the url becomes http://127.0.0.1:8000/admin/updatescholarship/50现在更新奖学金后,网址变为http://127.0.0.1:8000/admin/updatescholarship/50

50 is the scholarship id passed into the url 50是传入url的奖学金id

Now when I try to go to dashboard in my project, the url becomes http://127.0.0.1:8000/admin/updatescholarship/dashboard现在,当我尝试转到我的项目中的仪表板时,网址变为http://127.0.0.1:8000/admin/updatescholarship/dashboard

I dont't want the dashboard to get appended after the updatescholarship .我想dont't仪表板updatescholarship后得到追加。 The url should be http://127.0.0.1:8000/admin/dashboard网址应为http://127.0.0.1:8000/admin/dashboard

Here's my edit-scholarship view这是我的编辑奖学金视图

def admin_editscholarship(request):
    if request.method == 'POST':
        name = request.POST['sch_name']
        type = request.POST['sch_type']
        schdets = ScholarshipDetails.objects.filter(name = name,type = type)
        if schdets is not None:
            #if something exists in scholarship details, then print it
            print('Scholarship found')

    else:
        schdets = None

    return render(request,'admin-editscholarship.html',{'schdets':schdets})

Here's my update-scholarship view这是我的更新奖学金视图

def admin_updatescholarship(request,pk=None):
    #can update the new data in the selectd scholarship
    if pk:
        sch = ScholarshipDetails.objects.get(pk = pk)


    if request.method == 'POST':
        form = EditScholarshipForm(request.POST,instance=sch)
        if form.is_valid():
            form.save()
            print('\nform saved')
            args = {'form' : form}

            messages.success(request,'Successfully updated')
            return render(request,'admin-editscholarship.html',args)

Here's my urls.py这是我的urls.py

    path('admin/dashboard',views.admin_dash),
    path('admin/addscholarship',views.admin_addscholarship),
    path('admin/editscholarship',views.admin_editscholarship),
    url(r'^admin/updatescholarship/(?P<pk>\d+)$',views.admin_updatescholarship,name = 
    'updatescholarship'),
    path('admin/students',views.admin_students),
    path('admin/requests',views.admin_requests)

you can redirect to other url using django redirect您可以使用 django 重定向重定向到其他 url

from django.shortcuts import redirect

def fn_test(request):
    task here
    return redirect('path_to_redirect/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM