簡體   English   中英

單擊提交按鈕 django 后無法重定向到另一個頁面

[英]Not able to redirect to another page after clicking on submit button django

  • 所以,我在下面的代碼中實現了一些 post、get 和 update。 將值提交到數據庫后。 返回重定向不會將頁面更改為UserProfile.htmlurls.py中是user_profile
  • 代碼

urls.py

path('user_profile', views.user_profile, name="user_profile"),

views.py

def edit_profile(request):
    try:
        # checking if the user exist in UserProfile through the logged in email id
        user_data = UserProfile.objects.get(emailID = request.user.email)
        # if request.method == "POST" and request.FILES:
        if request.POST.get('action') == 'post':
            name = UserProfile.objects.all()
            response_data = {}
            # user_img = request.FILES['user_img']
            name = request.user.username
            emailID = request.user.email
            phone = request.POST.get('phone')
            college_name = request.POST.get('college_name')
            branch = request.POST.get('branch')

            # response_data['user_img'] = user_img
            response_data['name'] = name
            response_data['emailID'] = emailID
            response_data['phone'] = phone
            response_data['college_name'] = college_name
            response_data['branch'] = branch
            # updating the current logged in user values
            user_data = UserProfile.objects.get(emailID = request.user.email)
            if(user_data.emailID == request.user.email):
                UserProfile.objects.filter(emailID = request.user.email).update(
                    name = name,
                    emailID = emailID,
                    phone = phone,
                    college_name = college_name,
                    branch = branch
                )
            return redirect('/user_profile')
    except UserProfile.DoesNotExist:
        name = UserProfile.objects.all()
        response_data = {}
        # creating new user
        if request.POST.get('action') == 'post':
            # user_img = request.FILES['user_img']
            name = request.user.username
            emailID = request.user.email
            phone = request.POST.get('phone')
            college_name = request.POST.get('college_name')
            branch = request.POST.get('branch')

            # response_data['user_img'] = user_img
            response_data['name'] = name
            response_data['emailID'] = emailID
            response_data['phone'] = phone
            response_data['college_name'] = college_name
            response_data['branch'] = branch
            try:
                # checking if the user exist
                user_data = UserProfile.objects.get(emailID = request.user.email)
            except UserProfile.DoesNotExist:
                # if the user doesn't exist create the user
                UserProfile.objects.create(
                    name = name,
                    emailID = emailID,
                    phone = phone,
                    college_name = college_name,
                    branch = branch
                )
            return redirect('/user_profile')
    else:
        # if the profile is already created fetch the values
        context = {
            'name' : user_data.name,
            'emailID' : user_data.emailID,
            'phone' : user_data.phone,
            'college_name' : user_data.college_name,
            'branch' : user_data.branch
            }
        return render(request, 'edit_profile.html', {'context' : context})
    return redirect('/user_profile')
  • 我也嘗試過return HttpResponseRedirect 但這也不適合我。

添加

from django.core.exceptions import ObjectDoesNotExist

改變除了

except ObjectDoesNotExist:

並從重定向中刪除“/”

return redirect('user_profile')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM