python - Django Templatetag, request username -


In a template tag, in the template I am using request , I Trying to get the user's username by using {{user.username}} . When a similar code is not used in a template tag, but works in a view (using the request reference), but now it is not in the template tag, it does not show the username.

student_block.py (Templatetag)

  # template tag def student_block (request): progress = 20 user_id = request.user.id first_name = request.user.first_name last_name Try = request.user.last_name: studying_course = Student.objects.get (user__id = user_id). Course studying_year = Student.objects.get (user__id = user_id). Years excluding student No notes: studying_course = none studying_year = none if first_name and last_name: progress + = 30 if (studying_year and (study): = progress + = 50 returns {'progress': progress,' course ': studies Congress,' years ': Studying_year} register.inriage_tag (' study / student_block.html ') (student_block)   

student_block.html

   DivX = "Panel Panel - Default Sidebar"> 
gt; H3 Class = "Panel-Title" & gt; , {{User.username}} & lt; / h3 & gt; & lt; / div & gt; & lt; / div & gt;

The request is not a variable in that radius. You must do it before the reference

  @ register.inclusion_tag ('study / student_block.html', le_contact = true) def student_bloc (reference) : Request = reference ['request'] # You can use whatever you want   



Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -