Answer the question
In order to leave comments, you need to log in
How to pass data from one view to another and then to the template?
You need to forward the user_filter and datas data from def login_view to def index_view, so that this data can then be transferred to home.html
def login_view(request, *args, **kwargs):
form = UserLoginForm(request.POST or None)
test = 'asdasdasd'
if form.is_valid():
user_obj = form.cleaned_data.get('user_obj')
login(request, user_obj)
conn_string = "host='localhost' dbname='askue_rs_test' user='developer' password='developer'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
user = str(user_obj)
cursor.execute("SELECT filter_department FROM accounts_myuser WHERE username = %s", [user])
user_filter = cursor.fetchall()
elem = (str(user_filter).lstrip("[('").rstrip("\\',)]") + "%")
print(elem)
cursor.execute("SELECT filter_dep from test_data WHERE filter_dep LIKE %s", (elem,))
datas = cursor.fetchall()
#print(datas)
return HttpResponseRedirect("/")
return render(request, "registration/login.html", {"form": form})
def indexView(request):
return render(request, 'home.html')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question