Reputation: 11
When def login() function call and redirect to def index() function my url change in the browser and look like http://127.0.0.1:8000/error500index this url. But logically url look like http://127.0.0.1:8000/index this. But error500 show in the url when i use redirect function, error500 is my last url in the Project urls.py and APP urls.py.
Anyone help me out what is the happening?
view.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponse
from .models import WebUser
def index(request):
return render(request, 'index.html')
def login(request):
if (request.method == 'POST'):
login_email = request.POST['email']
login_password = request.POST['password']
# Compare with Database where input email exist!
try:
CheckUser = WebUser.objects.get(email=login_email)
except:
return HttpResponse("User Dosen't Exist!")
if (login_email == CheckUser.email and login_password == CheckUser.Password):
#When redirect function call my url change and pick the last url from project urls.py and this url appears in the browser http://127.0.0.1:8000/error500index
return redirect(index)
else:
return HttpResponse("Email or Password are wrong!")
else:
return render(request, 'login.html')
Project Urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('SIS_APP.urls')),
path('index', include('SIS_APP.urls')),
path('login', include('SIS_APP.urls')),
path('register', include('SIS_APP.urls')),
path('settings', include('SIS_APP.urls')),
path('weather', include('SIS_APP.urls')),
path('error404', include('SIS_APP.urls')),
path('error500', include('SIS_APP.urls')),
]
APP urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('index', views.index, name='index'),
path('login', views.login, name='login'),
path('register', views.register, name='register'),
path('settings', views.settings, name='settings'),
path('weather', weatherAPI.weather, name='weather'),
path('error404', views.error404, name='error404'),
path('error500', views.error500, name='error500'),
]
Upvotes: 1
Views: 413
Reputation: 24
I think you are not defining your URLs correctly. From the Django docs.
You will see that redirect can be used with a hardcoded link that is:
redirect("/index/")
And since you didn't add the slash in the URL we have:
redirect("index")
So it will pass the the value index to your URL. To fix it, add /
to your URL definition.
Upvotes: 0
Reputation: 46
You are missing the slashes "/" in your urls. URLs are concatenated, so if you have, i.e,
path('index', views.index, name='index'), ## this will give ..indexsome-sub-route
path('index/', views.index, name='index'), ## this will give ..index/some-sub-route
Upvotes: 1