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 9179831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:53:56+00:00 2026-06-17T17:53:56+00:00

Hello Im a django/python newbie and need some help understanding the code below. def

  • 0

Hello Im a django/python newbie and need some help understanding the code below.

def post_comment(request, new_comment):
    if request.session.get('has_commented', False):
        return HttpResponse("You've already commented.")
    c = comments.Comment(comment=new_comment)
    c.save()
    request.session['has_commented'] = True
    return HttpResponse('Thanks for your comment!')

Where I am confused is at if request.session.get(‘has_commented’,False)
If a user has commented the’has_commented’ key is set to true, so wouldnt you check to see if ‘has_commented’ is set to true to make sure a user doesnt post another comment instead of checking if ‘has_commented’ is false. Is my logic incorrect? Also where is the session key ‘has_commented’ originating from?

  • 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-06-17T17:53:57+00:00Added an answer on June 17, 2026 at 5:53 pm

    The function request.session.get takes 2 parameters namely

    • the key (‘has_commented’)
    • the default (False)

    The function will try return a value for the key, but if the key does not exist, or if the value is NULL then the default will be returned instead.

    In other words, if the value for ‘has_commented’ is True the if statement will run. However if there is no ‘has_comment’ key in the session, request.session.get will return the default which is False and the if statement will not run.

    The session key ‘has_commented’ originates in the line:

    request.session[‘has_commented’] = True

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm a newbie trying to use django to register some users, I have
hello i'm new in python and django I need a view that get current
views.py from django import HttpResponse def hello(request): return HttpResponse(Hello world ! ) Request Method:
Hello I need to have multiple language support of my django admin application.I can
Hello all I'm using Django 1.2.5 with Python 2.6 on two machines: Ubuntu 11.04
Hello I seem to be having some problem involving counting in Django. I have
I am a python/django newbie following along this tutorial on dotcloud . I git
I am having some problems with processing my form in python django when using
[python 2.6 - django 1.1.1] Hello. I'm writing a custom serializer for my django
I'm using Django with mod_python on remote server and need to add some logging.

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.