django - How should i call class based generic view in function view -
i want call class based index view in function view render index template in login function view when add context in index template function views ' render's context not 1 rendered class based index view.
so thought if call class based index view function add context class based view , render @ same time.
in short word's want add context function view , render it.
this class based generic view listing object's in index template.
class indexview(generic.listview): template_name = 'polls/index.html' title = 'hello' num_visit = 'a' context_object_name = 'question_list' def get_queryset(self): """return last 5 published questions.""" return question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5]
and login view render's same template class based index view
def login_view(request): user = authenticate(request,username=request.post['username'],password=request.post['password']) if user not none: login(request, user) return redirect('polls:index') return render_to_response('polls/index.html', {'error_message': 'wrong credentials'})
.................................................
now can login in index template @ code context adding does'nt workin maybe work's think messes default object list context.
class indexview(generic.listview): template_name = 'polls/index.html' title = 'sdasd' num_visit = 'a' context_object_name = 'question_list' error_message = none def get_queryset(self): """return last 5 published questions.""" return question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5] def post(self, request, **kwargs): user = authenticate(request, username=request.post['username'], password=request.post['password']) if user not none: login(request, user) return redirect('polls:index') error_message = 'i love jesus' context = self.get_context_data(request, error_message, **kwargs) return render(request, 'polls:index', context) def get_context_data(self, *args, **kwargs): context = super(indexview, self).get_context_data(**kwargs) context['error_message'] = self.args return context
template code .............. cant add template code ' it's complicated added here https://codeshare.io/5zxyjb
trackback error https://ibb.co/jda7ek
you should not call logic 1 view another. create login logic. didn't post code i'm not quite sure how program works this:
in forms.py
class myloginform(form): username = forms.textfield(...) password = forms.passwordfield(...)
in utils.py
def login(request, user_name, password): user = authenticate(request, username=user_name, password=password) if user not none: login(request, user) return true return false def index_context(...): context = {} # context stuff here return context
in views.py
class index(view): def post(self, request, *args, **kwargs): # logging_in part optional it's used know when post request login post or else. add hidden field in login form has name , value of true. if request.post.get("logging_in", false): authenticated = utils.login(request, request.post.get("username", ""), request.post.get("password", "")) if authenticated: # refresh page else: # refresh page error else: # other post actions. def get(self, request, *args, **kwargs): # add username , password form context along whatever other context need. context = utils.index_context(...) context["login_form"] = myloginform return render(request, "index.html", context) def login_view(request): # used if want separate login page too. if request.method == "post": loggedin = utils.login(request, request.post.get("username", ""), request.post.get("password", "")) if loggedin: return redirect('polls:login') return render_to_response('polls/login.html', {'error_message': 'wrong credentials'}) else: # render template username , password field context = {} context["login_form"] = myloginform return render(request, "login.html", context)
you have api view login stuff , have ajax call view. either way, actual logic logging in should in separate function global.
Comments
Post a Comment