Return Class based view from function based view












0















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")

]









share|improve this question




















  • 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 to Dashboard to help.

    – 2ps
    Nov 23 '18 at 19:25











  • I included my urls.py

    – Reez0
    Nov 23 '18 at 20:23
















0















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")

]









share|improve this question




















  • 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 to Dashboard to help.

    – 2ps
    Nov 23 '18 at 19:25











  • I included my urls.py

    – Reez0
    Nov 23 '18 at 20:23














0












0








0








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")

]









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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 to Dashboard 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












2 Answers
2






active

oldest

votes


















1














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






share|improve this answer


























  • 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











  • 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



















1














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/






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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






share|improve this answer


























  • 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











  • 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
















1














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






share|improve this answer


























  • 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











  • 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














1












1








1







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • 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











  • 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













1














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/






share|improve this answer
























  • 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
















1














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/






share|improve this answer
























  • 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














1












1








1







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/






share|improve this answer













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/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 6:53









Bidhan MajhiBidhan Majhi

468412




468412













  • 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



















  • 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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python