无法重定向到urls.py中的特定路径名

问题描述

我在Django项目中遇到一个问题,我想使用它的名称重定向到特定路径。我的代码如下所示。它不会重定向到路径名,但会完美地重定向http://127.0.0.1:8000/。我用谷歌搜索并且还在堆栈溢出中搜索了它,但是我没有找到与此相关的任何问题。有什么问题和解决方案。我遵循了教程,在那里他的代码可以正常工作并且重定向良好。

urls.py

from django.contrib import admin
from django.urls import path
from .views import *

urlpatterns = [
    path('',index,name='myhomepage'),path('order',order),path('signup',signup)
]

在views.py中,代码如下:

from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models.product import Product
from .models.category import Category
from .models.customer import Customer


# Create your views here.
def index(request):
    # calling get_all_product() method from products
    products = None
    categories = Category.get_all_categories()
    category_id_from_server = request.GET.get('category')
    if category_id_from_server:
        products = Product.get_all_product_by_category_id(category_id_from_server)
    else:
        products = Product.get_all_product()
    data = {}
    data['products'] = products
    data['categories'] = categories
    return render(request,'index.html',data)


def order(request):
    return render(request,'orders.html')


def signup(request):
    if request.method == 'GET':
        return render(request,'signup.html')
    else:
        postdata = request.POST
        first_name = postdata.get('firstname')
        last_name = postdata.get('lastname')
        phone_number = postdata.get('phone')
        email_id = postdata.get('email')
        pass_word = postdata.get('password')

        # Validation
        error_message = None
        formdata = {
            'firstname_formdata': first_name,'lastname_formdata': last_name,'phonenumber_formdata': phone_number,'emailid_formdata': email_id
        }
        if not first_name:
            error_message = "First Name required !!!"
        elif len(first_name) < 4:
            error_message = "First Name should be more than 4 character."
        elif not last_name:
            error_message = "Last Name required !!!"
        elif len(last_name) < 4:
            error_message = "last Name should be more than 4 character."
        elif not phone_number:
            error_message = "Phone Number required !!!"
        elif len(phone_number) < 5:
            error_message = "Phone Number should be more than 5 character."
        elif not email_id:
            error_message = "Email ID required !!!"
        elif not pass_word:
            error_message = "Password required !!!"
        elif len(pass_word) < 4:
            error_message = "Password should be more than 4 character."

        # Saving in Database
        # customer object = Customer (customer model name = Post data)
        if not error_message:
            customerobj = Customer(first_name=first_name,last_name=last_name,phone=phone_number,email=email_id,password=pass_word)
            customerobj.register()
            return redirect('myhomepage')
        else:
            data = {
                'error': error_message,'formdata_key': formdata
            }
            return render(request,'signup.html',data)

这应该将我重定向到主页,但不会按要求重定向

但如果我这样做

return redirect('http://127.0.0.1:8000/ ')

它将根据需要重定向

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)