Return Class based view from function based view
So I set up a login view, and upon successful login I'd like to go to a page of my choice by referencing another class based view. I'm not entirely sure how to achieve this.
Login view
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
else:
#TODO
else:
form = AuthenticationForm()
Dashboard class I'm trying to get to
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, request,**kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
urls.py
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash")
]
python django
add a comment |
So I set up a login view, and upon successful login I'd like to go to a page of my choice by referencing another class based view. I'm not entirely sure how to achieve this.
Login view
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
else:
#TODO
else:
form = AuthenticationForm()
Dashboard class I'm trying to get to
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, request,**kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
urls.py
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash")
]
python django
2
You redirect to URLs, not views. Show your URL patterns.
– Daniel Roseman
Nov 23 '18 at 17:42
1
We'll need to see youurls.py
where you assign a route toDashboard
to help.
– 2ps
Nov 23 '18 at 19:25
I included my urls.py
– Reez0
Nov 23 '18 at 20:23
add a comment |
So I set up a login view, and upon successful login I'd like to go to a page of my choice by referencing another class based view. I'm not entirely sure how to achieve this.
Login view
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
else:
#TODO
else:
form = AuthenticationForm()
Dashboard class I'm trying to get to
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, request,**kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
urls.py
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash")
]
python django
So I set up a login view, and upon successful login I'd like to go to a page of my choice by referencing another class based view. I'm not entirely sure how to achieve this.
Login view
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
else:
#TODO
else:
form = AuthenticationForm()
Dashboard class I'm trying to get to
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, request,**kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
urls.py
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash")
]
python django
python django
edited Nov 23 '18 at 20:23
Reez0
asked Nov 23 '18 at 16:47
Reez0Reez0
400417
400417
2
You redirect to URLs, not views. Show your URL patterns.
– Daniel Roseman
Nov 23 '18 at 17:42
1
We'll need to see youurls.py
where you assign a route toDashboard
to help.
– 2ps
Nov 23 '18 at 19:25
I included my urls.py
– Reez0
Nov 23 '18 at 20:23
add a comment |
2
You redirect to URLs, not views. Show your URL patterns.
– Daniel Roseman
Nov 23 '18 at 17:42
1
We'll need to see youurls.py
where you assign a route toDashboard
to help.
– 2ps
Nov 23 '18 at 19:25
I included my urls.py
– Reez0
Nov 23 '18 at 20:23
2
2
You redirect to URLs, not views. Show your URL patterns.
– Daniel Roseman
Nov 23 '18 at 17:42
You redirect to URLs, not views. Show your URL patterns.
– Daniel Roseman
Nov 23 '18 at 17:42
1
1
We'll need to see you
urls.py
where you assign a route to Dashboard
to help.– 2ps
Nov 23 '18 at 19:25
We'll need to see you
urls.py
where you assign a route to Dashboard
to help.– 2ps
Nov 23 '18 at 19:25
I included my urls.py
– Reez0
Nov 23 '18 at 20:23
I included my urls.py
– Reez0
Nov 23 '18 at 20:23
add a comment |
2 Answers
2
active
oldest
votes
In general, the way to redirect is by using the name of the url/route for the view you are using.
So if in your urls.py
you had something like:
urlpatterns = [
re_path('^dashboard$', Dashboard.as_view(), name='dashboard'),
]
You could reuse the name
part of the route to send the user a 302 redirect using redirect
:
from django.shortcuts import redirect
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
return redirect('dashboard') # matches the name part of the route in urls.py
else:
#TODO
else:
form = AuthenticationForm()
n.b., you also have an error in your view. There is no request
parameter to get_context_data
, so it should look like:
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:45
I've updated my answer to include an explanation of the error.
– 2ps
Nov 24 '18 at 16:56
Thanks for the help. Stupid error if you ask me, but at least I know now that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.
– Reez0
Nov 26 '18 at 14:55
add a comment |
Add redirect in your views.py,
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
return redirect('dash') #If you have mentioned app_name in urls.py add app_name:dash in place of dash
else:
#TODO
else:
form = AuthenticationForm()
In urls.py you have,
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash"),
]
This will redirect users to 127.0.0.1:8000/
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:44
Remove request from get_context_data()
– Bidhan Majhi
Nov 25 '18 at 5:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450383%2freturn-class-based-view-from-function-based-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In general, the way to redirect is by using the name of the url/route for the view you are using.
So if in your urls.py
you had something like:
urlpatterns = [
re_path('^dashboard$', Dashboard.as_view(), name='dashboard'),
]
You could reuse the name
part of the route to send the user a 302 redirect using redirect
:
from django.shortcuts import redirect
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
return redirect('dashboard') # matches the name part of the route in urls.py
else:
#TODO
else:
form = AuthenticationForm()
n.b., you also have an error in your view. There is no request
parameter to get_context_data
, so it should look like:
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:45
I've updated my answer to include an explanation of the error.
– 2ps
Nov 24 '18 at 16:56
Thanks for the help. Stupid error if you ask me, but at least I know now that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.
– Reez0
Nov 26 '18 at 14:55
add a comment |
In general, the way to redirect is by using the name of the url/route for the view you are using.
So if in your urls.py
you had something like:
urlpatterns = [
re_path('^dashboard$', Dashboard.as_view(), name='dashboard'),
]
You could reuse the name
part of the route to send the user a 302 redirect using redirect
:
from django.shortcuts import redirect
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
return redirect('dashboard') # matches the name part of the route in urls.py
else:
#TODO
else:
form = AuthenticationForm()
n.b., you also have an error in your view. There is no request
parameter to get_context_data
, so it should look like:
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:45
I've updated my answer to include an explanation of the error.
– 2ps
Nov 24 '18 at 16:56
Thanks for the help. Stupid error if you ask me, but at least I know now that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.
– Reez0
Nov 26 '18 at 14:55
add a comment |
In general, the way to redirect is by using the name of the url/route for the view you are using.
So if in your urls.py
you had something like:
urlpatterns = [
re_path('^dashboard$', Dashboard.as_view(), name='dashboard'),
]
You could reuse the name
part of the route to send the user a 302 redirect using redirect
:
from django.shortcuts import redirect
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
return redirect('dashboard') # matches the name part of the route in urls.py
else:
#TODO
else:
form = AuthenticationForm()
n.b., you also have an error in your view. There is no request
parameter to get_context_data
, so it should look like:
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
In general, the way to redirect is by using the name of the url/route for the view you are using.
So if in your urls.py
you had something like:
urlpatterns = [
re_path('^dashboard$', Dashboard.as_view(), name='dashboard'),
]
You could reuse the name
part of the route to send the user a 302 redirect using redirect
:
from django.shortcuts import redirect
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
#Not sure what to do next
#return HttpResponseRedirect(reverse(request, Dashboard))?
return redirect('dashboard') # matches the name part of the route in urls.py
else:
#TODO
else:
form = AuthenticationForm()
n.b., you also have an error in your view. There is no request
parameter to get_context_data
, so it should look like:
class Dashboard(ListView):
model = models.Note
template_name = 'notemanager/dashboard.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
notedata = models.Note.objects.filter(added_by = User)
reminderdata = models.Reminder.objects.filter(added_by = User)
context['notes'] = notedata
context['reminder'] = reminderdata
return context
edited Nov 24 '18 at 16:56
answered Nov 23 '18 at 19:30
2ps2ps
7,7622931
7,7622931
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:45
I've updated my answer to include an explanation of the error.
– 2ps
Nov 24 '18 at 16:56
Thanks for the help. Stupid error if you ask me, but at least I know now that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.
– Reez0
Nov 26 '18 at 14:55
add a comment |
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:45
I've updated my answer to include an explanation of the error.
– 2ps
Nov 24 '18 at 16:56
Thanks for the help. Stupid error if you ask me, but at least I know now that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.
– Reez0
Nov 26 '18 at 14:55
Okay that works but now I get
get_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:45
Okay that works but now I get
get_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:45
I've updated my answer to include an explanation of the error.
– 2ps
Nov 24 '18 at 16:56
I've updated my answer to include an explanation of the error.
– 2ps
Nov 24 '18 at 16:56
Thanks for the help. Stupid error if you ask me, but at least I know now that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.
– Reez0
Nov 26 '18 at 14:55
Thanks for the help. Stupid error if you ask me, but at least I know now that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.
– Reez0
Nov 26 '18 at 14:55
add a comment |
Add redirect in your views.py,
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
return redirect('dash') #If you have mentioned app_name in urls.py add app_name:dash in place of dash
else:
#TODO
else:
form = AuthenticationForm()
In urls.py you have,
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash"),
]
This will redirect users to 127.0.0.1:8000/
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:44
Remove request from get_context_data()
– Bidhan Majhi
Nov 25 '18 at 5:32
add a comment |
Add redirect in your views.py,
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
return redirect('dash') #If you have mentioned app_name in urls.py add app_name:dash in place of dash
else:
#TODO
else:
form = AuthenticationForm()
In urls.py you have,
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash"),
]
This will redirect users to 127.0.0.1:8000/
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:44
Remove request from get_context_data()
– Bidhan Majhi
Nov 25 '18 at 5:32
add a comment |
Add redirect in your views.py,
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
return redirect('dash') #If you have mentioned app_name in urls.py add app_name:dash in place of dash
else:
#TODO
else:
form = AuthenticationForm()
In urls.py you have,
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash"),
]
This will redirect users to 127.0.0.1:8000/
Add redirect in your views.py,
def login_view(request):
if request.method == 'POST':
form =AuthenticationForm(data=request.POST)
if form.is_valid():
user=form.get_user()
login(request,user)
return redirect('dash') #If you have mentioned app_name in urls.py add app_name:dash in place of dash
else:
#TODO
else:
form = AuthenticationForm()
In urls.py you have,
urlpatterns = [
path('login/',views.Login.as_view(),name="login"),
path('',views.Dashboard.as_view(), name ="dash"),
]
This will redirect users to 127.0.0.1:8000/
answered Nov 24 '18 at 6:53
Bidhan MajhiBidhan Majhi
468412
468412
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:44
Remove request from get_context_data()
– Bidhan Majhi
Nov 25 '18 at 5:32
add a comment |
Okay that works but now I getget_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:44
Remove request from get_context_data()
– Bidhan Majhi
Nov 25 '18 at 5:32
Okay that works but now I get
get_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:44
Okay that works but now I get
get_context_data() missing 1 required positional argument: 'request'
– Reez0
Nov 24 '18 at 13:44
Remove request from get_context_data()
– Bidhan Majhi
Nov 25 '18 at 5:32
Remove request from get_context_data()
– Bidhan Majhi
Nov 25 '18 at 5:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450383%2freturn-class-based-view-from-function-based-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You redirect to URLs, not views. Show your URL patterns.
– Daniel Roseman
Nov 23 '18 at 17:42
1
We'll need to see you
urls.py
where you assign a route toDashboard
to help.– 2ps
Nov 23 '18 at 19:25
I included my urls.py
– Reez0
Nov 23 '18 at 20:23