This is a book exchange system I'm trying to create. The challlenge I'm having is trying to redirect the user to a dynamic url consisting of there personal details after log in.
Here is my urs.py for my project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('exchange/',include('exchange.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
]
Here is my urs.py for my app
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('', views.base, name='base'),
path('register/', views.register_request, name='register'),
path('accounts/<int:id>/', views.user_view, name='userview'),
#path('login/', views.login_request, name='login'),
path('login/', auth_views.LoginView.as_view(template_name='exchange/login.html', redirect_field_name='user_view')),
]
Here is my views.py for my app
from django.shortcuts import render, redirect
from exchange.forms import user_login_form, user_register_form
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.contrib import messages
# Create your views here.
#Base index view
def base(request):
return render(request, 'exchange/base.html',{})
#registration view
def register_request(request):
if request.method=='POST':
form = user_register_form(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = User.objects.create_user(username=username, email=email, password=password)
user.save()
return redirect('login')
else:
messages.error(request, 'Invalid form')
render(request, 'exchange/register.html',{'form':form})
else:
form = user_register_form()
return render(request, 'exchange/register.html',{'form':form})
#login view
def login_request(request):
if request.method=='POST':
form = user_login_form(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect('user_view', id=user.id)
else:
messages.error(request, 'Invalid username or password')
render(request, 'exchange/login.html',{'form':form})
else:
messages.error(request, 'Invalid form')
render(request, 'exchange/login.html',{'form':form})
form = user_login_form()
return render(request, 'exchange/login.html',{'form':form})
#userview
def user_view(request, id):
user = User.objects.get(id=id)
return render(request, 'exchange/user_view.html',{'user':user})
I have also added LOGIN_REDIRECT_URL = '/accounts/int:id/' to my settings.py.
CodePudding user response:
I presume your app is called exchange
because you've put this 'accounts/<int:id>'
inside your exchange app's urls, it's actually trying to match for 'exchange/accounts/<int:id>'
path('exchange/',include('exchange.urls')),
# Link App's Urls -> 'exchange/{x}'.format(pattern)
# exchange.urls (prepend 'exchange')
# url: exchange
path('', views.base, name='base'),
# url: exchange/register
path('register/', views.register_request, name='register'),
# url: exchange/accounts/<int:id>
path('accounts/<int:id>/', views.user_view, name='userview'),
# etc
There's really no way around this prepending, so i think your best bet would be to put it in the main urls and just explicitly say what app view
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
from exchange.views import user_view
urlpatterns = [
path('exchange/',include('exchange.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/<int:id>', user_view, name='userview'),
path('admin/', admin.site.urls),
]
CodePudding user response:
I do not see your models but are you sure that you use user_id like
<int:id>
Normally I use pk like this
<int:pk>
CodePudding user response:
You have the following name in your URLs.py
path('accounts/<int:id>/', views.user_view, ***name='userview'***),
But you are using a different name (with an underscore) in your redirect
return redirect(***'user_view'***, id=user.id)
They will need to match for the redirect to work