私は自分自身にDjangoを教えています。既存のユーザーが自分の「もの」をリストしたページを取得し、新しいユーザーが自分の「もの」を通知するページを取得するようにテストを試みています。システムに存在せず、ユーザーのリストを生成できます。問題は、既存のユーザー辞書にないユーザーに対してKeyErrorsが発生し続けることです(ユーザーが辞書に含まれている場合は正常に機能します)。問題がviews.pyで関連する関数を構造化した方法にあるのか、templateTagsを使用している方法にあるのか、または何なのかわかりません。Djangoのエラーページは私のviews.pyページのコンテキスト行を指していますが、それが実際に問題であるかどうかはわかりません。
どんな助けでも大歓迎です。
私のコード:
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 %}
そして最後に:
urls.py:
...
urlpatterns = ('',
(r'^user/name/(.*)/$',hiUser),
)