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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:29:24+00:00 2026-06-16T21:29:24+00:00

I have a view that looks like this: def CineByCiudad(request): city = request.session[ciudad] cines

  • 0

I have a view that looks like this:

def CineByCiudad(request):
    city = request.session["ciudad"]
    cines = Cine.objects.filter(ciudad=city)
    context = {'cines': cines}
    return render_to_response('cine-ciudad.html', context, context_instance=RequestContext(request))

Now, I am using the session variable “ciudad” to filter my query.
On the homepage of my site I let the user set their “ciudad” and once its set I redirect them to another page and they can start viewing the content based on their city(“ciudad”).

My homepage checks if the session has items:

def index(request):
    #Checks to see if the session has items, in particular if a city has been set
    if not request.session.items():
        return render_to_response('index.html', context_instance=RequestContext(request))
    else:
        return redirect(Cartelera)

Now lets suppose for some reason the user deletes his cookies and on the site he visits a url different than the homepage(something like http://www.site.com/cartelera) he would then get an error because the session variable “ciudad” is not set.

Is there a way to create a default value for this session variable in case that it has not been set?
Or what is a good practice to deal with this issue?

  • 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-16T21:29:25+00:00Added an answer on June 16, 2026 at 9:29 pm

    Use the dict.get method on the session to retrieve a default if the value isn’t set, like this:

    city = request.session.get('ciudad', 'default value')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that looks like this: def CarteleraPeliculaToday(request, slugpelicula): city = request.session[ciudad]
I have a view that looks like this: def CarteleraPeliculaToday(request, slugpelicula): city = request.session[ciudad]
I have a view that looks like this: def selectCity(request, the_city): request.session[ciudad] = the_city
I have a django view that looks like... def add_user(request): if User.objects.get(username__exact = request.POST['username']):
I have a view that looks like this: def login(request): ... # some variables
I have a view model that looks like this public class ViewModelRound2 { public
I have a help method that looks a something like this: def html_format(text, width=15,
I have a link that looks like this in the object's show view: <%=
I had a function based view that looked like this: def account_details(request, acc_id): account
I have a sub form that looks like a datasheet view. It returns prices

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.