python - How to use the same auth userprofile in multiple apps in Django 1.6 -
i'm using django 1.6 auth module login users , have project consisting of 2 apps , want use same user calling request.user.username in view in second app. is, in template login.html in first app works {% if user.is_authenticated %} {% if user.is_authenticated %} not work in second apps templates , i'm wondering how can fetch in functions in second app? i've tried fetch user=request.user.username doesn't work.
in first app:
models.py
from django.contrib.auth.models import user class userprofile(models.model): user = models.onetoonefield(user) title = models.charfield(max_length = 70) def __unicode__(self): return self.title class oneuser(models.model): firstname = models.charfield(max_length = 35) lastname = models.charfield(max_length = 35) email = models.emailfield() belongsto = models.foreignkey(userprofile, related_name='pcslabeler_userprofile') def __unicode__(self): return self.firstname
in views.py
def loginindex(request): c = {} c.update(csrf(request)) return render(request,"login.html", c) #logins = login.objects.all() def one_login(request): username = request.post.get('username', '') password = request.post.get('password', '') user = auth.authenticate(username=username, password=password)# user firstname lastname if user not none: auth.login(request, user) return httpresponseredirect('/loggedin')# topic else: return httpresponseredirect('/invalid') def loggedin(request): return render(request,"loggedin.html",{'full_name': request.user.username}) def invalid_login(request): return render(request,"invalid_login.html") def logout(request): auth.logout(request) return render(request,"logout.html")
in second apps view i'm trying use in function postoneeval:
def evalindex(request): evals = evaluation.objects.all() return render_to_response("evaluation_index.html",{"evals":evals}) def one_evaluation(request, postid): one_eval = evaluation.objects.get(id=postid) one_part = participation.objects.get(id=postid) if request.method == 'post': form = evalform(request.post) form2 = participationform(request.post) if form.is_valid() , form2.is_valid(): postoneeval(request, one_eval) postoneparticipant(request, one_part) else: form = evalform() form2 = participationform() c = {"one_eval":one_eval,"one_part":one_part, "form":form, "form2":form2} c.update(csrf(request)) return render_to_response("one_evaluation_index.html", c ) def postoneeval(request, one_eval): title = request.post["title"] user = request.user.username oneeval = oneevaluation(datetime=datetime.datetime.now(), belongsto=one_eval, title=title, user=user) oneeval.save()
the problem in render_to_reponse()
call. not pass requescontext
instance context processors aren't run templates.
the simplest solution issue replace render_to_response()
render()
:
return render(request, "evaluation_index.html", {"evals":evals})
and:
return render(request, "one_evaluation_index.html", c)
Comments
Post a Comment