Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:10:19+00:00 2026-05-26T16:10:19+00:00

Im using Django 1.3.1.In my django code, i have the following template for the

  • 0

Im using Django 1.3.1.In my django code, i have the following template for the main page, its called main_page.html:

<html>
<head>
<title>My Site</title>
</head>
<body>
<h1>Welcome</h1>
{% if user.username %}
   <p>Welcome {{ user.username }}!</p>
{% else %}
   <p>Welcome anonymous user!
   You need to <a href="/login/">login</a>
{% endif %}
</body>
</html>

I have the following view for this template:

def main_page(request):
  template = get_template('main_page.html')
  variables = Context({'user': request.user})
  output = template.render(variables)
  return HttpResponse(output)

This works as expected,i.e. it checks if the user has already logged in or not and greets accordingly. But if i replace the above view with the code below, then i always get the message for anonymous user on the main page, irrespective of whether i have logged in or not.

def main_page(request):
  return render_to_response(
      'main_page.html',
       {'user': request.user}
  )

What could be going wrong here ?. Please help.

Thank You

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:10:19+00:00Added an answer on May 26, 2026 at 4:10 pm

    In the template you should use {% if request.user.is_authenticated %} instead of your {% if user.username %}. That should solve your problem.

    On the other hand, I don’t know, why you’re trying to explicitly add the user variable in the view. Why not using something like:

    def main_page(request):
      return render_to_response(
          'main_page.html',
           context_instance=RequestContext(request)
      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is using the web app framework, not Django. The following template code is
I have the following code, using Django ORM routes =Routes.objects.filter(scheduleid=schedule.id).only('externalid') t_list = [(route.externalid, route.vehicle.name)
I have the following code in Django: import locale locale.setlocale( locale.LC_ALL, '' ) def
I have the following code in the urls.py in mysite project. /mysite/urls.py from django.conf.urls.defaults
I'm using Django, and I have the following error: Exception Type: SyntaxError Exception Value:
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I'm trying to create my first application using Django. I'm using the following code
I have the following code, using Crockford's json2 to parse the object into json
I have the following code in one of my Django templates that I want
I've been using Komodo Edit for a small project in Django. The code completion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.