I have a issue in django project where I want to redirect to a specific path using its name. My code looks like this. It doesn't redirect to a path name but It redirect towards http://127.0.0.1:8000/ perfectly. I googled and also searched it in stack overflow but I didn't find any question related to this. What can be the problem and solution to it. I followed tutorial and there his code his working fine and redirecting well.
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)
]
And in views.py the code goes like this:
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)
This should redirect me to the homepage but it don't redirect as required.
but if I do
return redirect('http://127.0.0.1:8000/ ')
It will redirect as required.