I grasp that this code isn't working because the request is somehow insufficient, but I'm uncertain as to how to go about correcting it. I'm trying to configure it so that, if the ajax data matches the username, it will redirect to home.html. Any help would be so greatly cherished! (PS: the print calls are working - if the user exists, it prints the "else" statement and vice versa. Nevertheless, the redirect still doesn't work!)
views.py
def profile(request):
profname = request.GET.get('profname', None)
obj = User.objects.filter(username=profname)
if not obj:
print("I need this time to redirect to error page")
else:
print ("I need this to redirect home")
return redirect('home')
return render(request, 'profile.html')
html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<h1>jkkk</h1>
<a id="butt" href="#">Get Data</a>
</body>
<script>
$(document).ready(function(){
$("#butt").click(function(){
$.ajax({
type: "get",
url: 'profile',
datatype:'json',
data: {
'profname': 'jillsmith',
},
});
});
});
</script>
urls.py
urlpatterns = [
path('', views.explore, name='explore'),
path('happening/profile/', views.profile, name='profile'),
path('happening/', views.happening, name='happening'),
path('home/', views.home, name='home'),
path('happening/test/', views.test, name='test'),
path('home/likes/', views.likes, name='likes'),
]