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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:35:07+00:00 2026-06-09T00:35:07+00:00

I’m teaching myself Django, and I’m trying to do a test thing such that

  • 0

I’m teaching myself Django, and I’m trying to do a test thing such that a pre-existing user will get a page listing his or her “stuff,” a new user will get a page informing him or her that they’re not in the system, and a list of users can be generated. The problem is that I keep getting KeyErrors for any users not in the pre-existing users dictionary (it works fine if the user is in the dict). I’m not sure if the problem is in the way I’ve structured the relevant function in my views.py or in the way I’m using the templateTags, or what. Django’s error page points to the context line of my views.py page, but I don’t know if that’s actually the problem.

Any help would be greatly appreciated.

My code:

views.py:

def hiUser(request,uname):
    t = get_template("samplate1.html")
    ds,ti = getTime()
    user_stuff = {"sam":["a","b","c"],"kathy":["foo","bar"],"rob":[]}
    c = Context({"date":ds,"time":ti,"user":uname,"user_stuff":user_stuff[uname],"users":user_stuff.keys()})
    return HttpResponse(t.render(c))

samplate1.html:

<html>
{% ifequal user "list" %}
<head><title>List of Users</title></head>
<body><h1>List of users</h1>
<ul>
{% for user in users %}
    <li>{{ user }}</li>
{% empty %}
    <p>No users listed!</p>
{% endfor %}
</ul>
{% endifequal %}
{% if user in users %}
<head><title>Greetings, {{ user }}</title></head>
<body>
<h1>Hello</h1>
<p>Greetings, {{ user }}</p>
<p>The date is {{ date }}</p>
<p>The time is {{ time }}</p>
<p>Here is a list of your stuff:</p>
<ul>
    {% for item in user_stuff %}
        <li>{{ item }}</li>
    {% empty %}
        <p>You don't have any stuff!</p>    
    {% endfor %}
    </ul>
{% else %}
<head><title>You're new here, huh?</title></head>
<body>
<h1>Hello</h1>
<p>Your username is not in our database.</p>
<p>You should probably fix that.</p>
{% endif %}

and finally:

urls.py:

...
urlpatterns = ('',
   (r'^user/name/(.*)/$',hiUser),
)
  • 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-09T00:35:09+00:00Added an answer on June 9, 2026 at 12:35 am

    Instead of user_stuff[uname] use user_stuff.get(uname). If the uname key doesn’t exist, the value will be None. Or you can use user_stuff.get(uname, []), which will make it an empty list if it doesn’t exist.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.