Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法重定向到URL.py中的特定路径名_Python_Django_Redirect_Path_Pathname - Fatal编程技术网

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

Python 无法重定向到URL.py中的特定路径名,python,django,redirect,path,pathname,Python,Django,Redirect,Path,Pathname,我在django项目中遇到了一个问题,我想使用它的名称重定向到一个特定的路径。我的代码看起来像这样。它不会重定向到路径名,但会重定向到路径名。我在谷歌上搜索过,也在StackOverflow中搜索过,但没有找到任何与此相关的问题。可能存在的问题和解决方案。我遵循教程,他的代码工作良好,重定向良好 url.py 在views.py中,代码如下所示: from django.shortcuts import render, redirect from django.http import HttpR

我在django项目中遇到了一个问题,我想使用它的名称重定向到一个特定的路径。我的代码看起来像这样。它不会重定向到路径名,但会重定向到路径名。我在谷歌上搜索过,也在StackOverflow中搜索过,但没有找到任何与此相关的问题。可能存在的问题和解决方案。我遵循教程,他的代码工作良好,重定向良好

url.py

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

它将根据需要重定向。

您是否收到任何错误而不是重定向?如果要在主模板中重定向用户,只需使用重定向(“/”)无任何错误。但它将重定向到表单的action=“/signup”中列出的/signup页。@非常感谢。这很有效,但为什么路径名不起作用?@MilanAdhikari Good你有没有收到任何错误而不是重定向?如果要在主模板中重定向用户,只需使用重定向(“/”)无任何错误。但它将重定向到表单的action=“/signup”中列出的/signup页。@非常感谢。这很有效,但为什么路径名称不起作用?@MilanAdhikari good
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/ ')