作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个设置页面,该页面具有用于处理两个不同模型的设置的两种形式。 Profile模型表单起作用。 Chef模型表格没有。表单正常失败,并且没有引发Django错误页面-因此在使用pdb时,我发现该表单无效,并且引发了语法错误。
对于该错误来自哪个 Realm ,我感到困惑。任何帮助将不胜感激。谢谢!
错误:
*** SyntaxError: SyntaxError('unexpected EOF while parsing', ('<string>', 0, 0, ''))
{% if form.is_multipart %}
<form enctype="multipart/form-data" method="post" action=".">{% csrf_token %}
{% else %}
<h3>Profile Settings</h3>
<form method="post" action=".">
{% endif %}
<dl>
<dt>{{form.photo.label}}</dt>
<dd>{{form.photo}}</dd>
<dt>{{form.firstname.label}}</dt>
<dd>{{form.firstname}}</dd>
<dt>{{form.lastinitial.label}}</dt>
<dd>{{form.lastinitial}}</dd>
</dl>
<button type="submit">Save</button>
</form>
<h3>Chef Settings</h3>
<form action="{% url edit_chef chef.id %}" method="post" accept-charset="utf-8">{% csrf_token %}
<dl>
<dt>{{chefform.category.label}}</dt>
<dd>{{chefform.category}}</dd>
<dt>{{chefform.price.label}}</dt>
<dd>{{chefform.price}}</dd>
<dt>{{chefform.meal.label}}</dt>
<dd>{{chefform.meal}}</dd>
<dt>{{chefform.language.label}}</dt>
<dd>{{chefform.language}}</dd>
<dt>{{chefform.address.label}}</dt>
<dd>{{chefform.address}}</dd>
<dt>{{chefform.neighborhood.label}}</dt>
<dd>{{chefform.neighborhood}}</dd>
<dt>{{chefform.city.label}}</dt>
<dd>{{chefform.city}}</dd>
<dt>{{chefform.state.label}}</dt>
<dd>{{chefform.state}}</dd>
<dt>{{chefform.menu.label}}<span id="rBann" class="minitext">1000</span></dt>
<dd>{{chefform.menu}}</dd>
</dl>
<button type="submit">Save</button>
</form>
class ChefForm(forms.ModelForm):
class Meta:
model = Chef
fields = ('category','meal','price','language','address','neighborhood','city','state', 'country', 'menu')
category = forms.ChoiceField(
label=_("Food style"),
choices=([('Afghan','Afghan'),('African','African'),('American','American'),]),
required=True)
meal = forms.ModelMultipleChoiceField(
label=_("What is your best meal?"),
queryset=Meal.objects.all(),
required=True)
price = forms.IntegerField(
label=_("Price per person"),
widget=forms.TextInput(),
required=True)
language = forms.ModelMultipleChoiceField(
label=_("Languages spoken"),
queryset=Language.objects.all(),
required=True)
address = forms.CharField(
label=_("Your Address"),
widget=forms.TextInput(),
required=True)
neighborhood = forms.CharField(
label=_("Your Neighborhood"),
widget=forms.TextInput(),
required=True)
city = forms.CharField(
label=_("Your City"),
widget=forms.TextInput(),
required=True)
state = forms.CharField(
label=_("Your state"),
widget=forms.TextInput(),
required=True)
country = forms.CharField(
label=_("Your country"),
widget=forms.TextInput(),
required=True)
menu = forms.CharField(
label=_("What's unique about your cooking & home? Pets? Have a family or roommates?"),
widget=forms.TextInput(),
required=True)
def __init__(self, *args, **kwargs):
super(ChefForm, self).__init__(*args, **kwargs)
self.fields['price'].widget.attrs = {
'placeholder':'10'}
self.fields['menu'].widget.attrs = {
'placeholder':'Tacos!'}
@login_required
def edit_chef(request, chef_id, template_name="chef/newchef.html"):
chef = get_object_or_404(Chef, id=chef_id)
if request.user != chef.cook:
return HttpResponseForbidden()
if request.method == 'POST':
import pdb; pdb.set_trace()
chefform = ChefForm(request.POST, instance=chef)
if chefform.is_valid():
chefform.save()
return HttpResponseRedirect('/users/%d/' % request.user.id)
else:
return HttpResponseRedirect('/users/%d/' % request.user.id)
data = { "chef":chef,
"chefform":chefform }
return render_to_response(template_name,
data,
context_instance=RequestContext(request))
[29/Jan/2011 09:20:24] "POST /chef/1/edit/ HTTP/1.1" 200 104804
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 281, in run
self.finish_response()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 321, in finish_response
self.write(data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 400, in write
self.send_headers()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 464, in send_headers
self.send_preamble()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 382, in send_preamble
'Date: %s\r\n' % http_date()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 322, in write
self.flush()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 301, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 53340)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 562, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 641, in __init__
self.finish()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 694, in finish
self.wfile.flush()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 301, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
[29/Jan/2011 09:20:27] "GET /users/2/ HTTP/1.1" 200 114593
最佳答案
SyntaxError::SyntaxError来自Python解析器本身。我认为这与您的表单数据没有任何关系。
我会尝试在此代码上运行pylint,pyflakes或pep8 compliance,并查看其内容。
我怀疑解析您的一个Python文件(可能是上面显示的文件之一的某个部分)时发生错误,但不完全是您所拥有的文件。例如,一个流浪(或[或{字符在某处可能会导致这种问题,因为在调用前不执行Form代码。
ChefForm声明的“required = True”部分上的奇数缩进支持此猜测。由于某些问题,您的编辑器缩进不正确,可能超出了我们在此处看不到的“类ChefForm”的整个声明。
关于python - Django : SyntaxError 'unexpected EOF' while saving a Form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4832741/
我是一名优秀的程序员,十分优秀!