2011-01-28 52 views
1

我有一個設置頁面,它有兩種形式來處理兩種不同模型的設置。簡介模型表單起作用。廚師模型表單沒有。表單失敗,並沒有拋出一個Django錯誤頁面 - 所以在使用pdb時,我發現表單無效,並且拋出了一個語法錯誤。Django:保存表單時語法錯誤'意外的EOF'

我很困惑這個錯誤來自哪個字段。任何幫助將不勝感激。謝謝!

錯誤:

*** SyntaxError: SyntaxError('unexpected EOF while parsing', ('<string>', 0, 0, '')) 

HTML

{% 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 
+0

你究竟輸入PDB來得到這個錯誤?如果運行沒有參數的pdb打印命令,則創建該錯誤。 – 2011-01-28 20:48:53

+0

直到'if chefform.is_valid():'打印錯誤 – Emile 2011-01-28 20:54:21

回答

0

的SyntaxError :: SyntaxError錯誤是來自於Python語法分析器本身。我認爲這與你的表單數據沒有任何關係。

我想在這段代碼上運行pylintpyflakespep8 compliance,看看它說了什麼。

我懷疑解析你的一個Python文件,可能是上面顯示的文件之一的一部分,但不完全是你在那裏。例如,一個流浪(或[或{字符某處可能會導致此類問題,因爲表單代碼不會執行直到調用。

ChefForm聲明支持的「required = True」部分上的奇數縮進這個猜測,你的編輯器因爲某些問題而錯誤地縮進了,可能高於整個「class ChefForm」的聲明,我們在這裏看不到這個

0

編輯:對不起,我錯了我的最後答覆。我正在想別的東西。

更換%s與%d在這條線:

return HttpResponseRedirect('/users/%s' % request.user.id)