您好我为运动员设计了一个注册体育比赛的注册系统。注册页面捕获所有运动员的信息并将其保存在运动员的数据库中。模型。然后,运动员电子邮件地址(模型中的唯一字段)被存储为会话变量,并且用户被重定向到支付页面。支付页面将从传递给页面的参数中获取事件信息,然后从会话变量中提取运动员的电子邮件地址,并使用该信息获取运动员的信息。从那里,它使用运动员和事件信息查询数据库以查找费用并将其作为注册费呈现给用户。
注册页面似乎没什么问题,但是我得到了一个"类型'事件'从注册页面重定向到付款页面时,无法迭代" 错误。
以下是付款页面的视图。
def payment(request, event_year, event_slug):
event = Event.objects.get(event_date__year = event_year, event_slug = event_slug)
athlete = Athlete.objects.get(email_address=request.session.get('user_email'))
if request.method == 'POST':
form = PaymentForm(data=request.POST)
if form.is_valid():
# process form payment data
form.save()
return redirect('home')
else:
form = PaymentForm(event, athlete)
return render(request, 'events/event_payment.html', {
'event': event,
'athlete': athlete,
'form': form
})
付款表格和登记表格都是常规表格,而不是ModelForms。这是付款表单的模板。请注意,如果从模板中删除{{form}}标记,则页面呈现时没有错误,但我需要{{form}}来处理付款。
{% extends "base.html" %}
{% block head %}
<title>{{ event.event_date.year }} {{ event.event_name }} Payment</title>
{% endblock head %}
{% block content %}
<div class="row-fluid">
<div class="span12">
<h2>{{ event.event_date.year }} {{ event.event_name }} Entry Form for {{ athlete.first_name }} {{ athlete.last_name }}</h2>
<form action="" method="post">
{% csrf_token %}
<p>Payment for {{ athlete.first_name }} {{ athlete.last_name }}
{{ form }}
<button type="submit">Register</button>
</form>
</div>
</div>
{% endblock content %}
为了完成起见,这里是注册页面的相关部分,虽然我不认为这很重要:
def register(request, event_year, event_slug):
event = Event.objects.get(
event_slug = event_slug,
event_date__year = event_year
)
if not event:
raise Http404
if request.method == 'POST':
form = EntryForm(data=request.POST, event=event)
if form.is_valid():
from django.core.exceptions import ObjectDoesNotExist
try:
athlete = Athlete.objects.get(email_address=form.cleaned_data['email_address'])
athlete.first_name = form.cleaned_data['first_name']
athlete.last_name = form.cleaned_data['last_name']
except ObjectDoesNotExist:
# code to create new athlete object
request.session['user_email'] = athlete.email_address
return redirect('payment', event_slug=event_slug, event_year=event_year)
else:
form = EntryForm(request.POST)
return render(request, 'events/event_register.html', {'form': form})
return render(request, 'events/event_register.html', {
'event': event,
'form': form
})
这是完整的回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/2014/bikini-figure-fitness-physique-championships/payment
Django Version: 1.5.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'events')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template error:
In template /home/nse/Development/Django/cbbf/events/templates/events/event_payment.html, error at line 17
argument of type 'Event' is not iterable
7 : {% block content %}
8 :
9 : <!-- Example row of columns -->
10 : <div class="row-fluid">
11 : <div class="span12">
12 : <h2>{{ event.event_date.year }} {{ event.event_name }} Entry Form for {{ athlete.first_name }} {{ athlete.last_name }}</h2>
13 : <p></p>
14 : <form action="" method="post">
15 : {% csrf_token %}
16 : <p>Payment for {{ athlete.first_name }} {{ athlete.last_name }}
17 : {{ form }}
18 : <button type="submit">Register</button>
19 : </form>
20 : </div>
21 : </div>
22 :
23 : {% endblock content %}
24 :
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/nse/Development/Django/cbbf/events/views.py" in payment
164. 'form': form
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
177. return t.render(context_instance)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
140. return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
830. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
124. return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
830. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
63. result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
830. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render
87. output = force_text(output)
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_text
99. s = s.__unicode__()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in __str__
100. return self.as_table()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in as_table
222. errors_on_separate_row = False)
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _html_output
145. top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in non_field_errors
248. return self.errors.get(NON_FIELD_ERRORS, self.error_class())
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _get_errors
117. self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in full_clean
272. self._clean_fields()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _clean_fields
281. value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in value_from_datadict
522. if name not in data:
Exception Type: TypeError at /2014/bikini-figure-fitness-physique-championships/payment
Exception Value: argument of type 'Event' is not iterable