4

我正在做一個應用程序與Django非相對谷歌App Engine,即時通訊使用Bootstrap ...在Django形式上使用Bootstrap我安裝django_forms_bootstrap(https://github.com/pinax/django-forms-bootstrapAttributeError:'str'對象沒有屬性'fields'使用Django不相關GAE

的事情是什麼?當我部署在GAE上,並試圖創建一個新用戶(註冊)我得到的服務器錯誤(500),但新用戶創建...日誌GAE的顯示此:

> Exception in request: Traceback (most recent call last): File 
> "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/core/handlers/base.py", 
> line 113, in get_response 
>  response = callback(request, *callback_args, **callback_kwargs) File 
> "/base/data/home/apps/s~softsystemanager/1.378394621720949564/myapp/modulos/presentacion/views.py", 
> line 32, in signup_view 
>  return render_to_response('presentacion/login.html', context_instance=RequestContext(request)) File 
> "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/shortcuts/__init__.py", 
> line 29, in render_to_response 
>  return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/loader.py", 
> line 177, in render_to_string 
>  return t.render(context_instance) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 140, in render 
>  return self._render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 134, in _render 
>  return self.nodelist.render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 830, in render 
>  bit = self.render_node(node, context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 844, in render_node 
>  return node.render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/loader_tags.py", 
> line 124, in render 
>  return compiled_parent._render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 134, in _render 
>  return self.nodelist.render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 830, in render 
>  bit = self.render_node(node, context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 844, in render_node 
>  return node.render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/loader_tags.py", 
> line 63, in render 
>  result = block.nodelist.render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 830, in render 
>  bit = self.render_node(node, context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 844, in render_node 
>  return node.render(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 881, in render 
>  output = self.filter_expression.resolve(context) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django/template/base.py", 
> line 606, in resolve 
>  new_obj = func(obj, *arg_vals) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django_forms_bootstrap/templatetags/bootstrap_tags.py", 
> line 20, in as_bootstrap 
>  form = _preprocess_fields(form) File "/base/data/home/apps/s~softsystemanager/1.378394621720949564/django_forms_bootstrap/templatetags/bootstrap_tags.py", 
> **line 10, in _preprocess_fields 
>  for field in form.fields: AttributeError: 'str' object has no attribute 'fields'** 

然後我去文件bootstrap_tags.py,我看不出有任何錯誤

> def _preprocess_fields(form): 
>  for field in form.fields: 
>   name = form.fields[field].widget.__class__.__name__.lower() 
>   if not name.startswith("radio") and not name.startswith("checkbox"): 
>    form.fields[field].widget.attrs["class"] = " form-control" 
>  return form 
> 
> 
> @register.filter def as_bootstrap(form): 
>  template = get_template("bootstrap/form.html") 
>  form = _preprocess_fields(form) 
> 
>  c = Context({ 
>   "form": form, 
>  }) 
>  return template.render(c) 

在我的應用程序的HTML文件,我得到這個

<div class="col-md-4 "> 
      <form action="." method="POST"> 
       {%csrf_token%} 
       {{ form|as_bootstrap}} 
       <button type="submit" class="btn btn-default">Sign UP</button> 
      </form>  
     </div> 
    </div> 

,我不知道這是否是有用的,但這裏是形式

class RegisterForm(forms.Form): 
    username = forms.CharField(label="Nombre de Usuario",widget=forms.TextInput()) 
    name = forms.CharField(label="Nombre",widget=forms.TextInput()) 
    last_name = forms.CharField(label="Apellido",widget=forms.TextInput()) 
    email = forms.EmailField(label="Correo Electronico", widget=forms.TextInput()) 
    password_one = forms.CharField(label="Password", widget=forms.PasswordInput(render_value=False)) 
    password_two = forms.CharField(label="Confirmar Password", widget=forms.PasswordInput(render_value=False)) 

事實上,錯誤的是這個特殊的形式,因爲我有另一個,登錄表單,它完美的作品,即時通訊使用相同的程序來使用django_forms_bootstrap

我的si的源代碼ngup_view是:

def signup_view(request): 
    form = RegisterForm() 
    if request.method == "POST": 
     form = RegisterForm(request.POST) 
     if form.is_valid(): 
      username = form.cleaned_data['username'] 
      name = form.cleaned_data['name'] 
      last_name = form.cleaned_data['last_name'] 
      email = form.cleaned_data['email'] 
      password_one = form.cleaned_data['password_one'] 
      password_two = form.cleaned_data['password_two'] 
      newUser = User.objects.create_user(username=username, first_name=name, last_name=last_name, email=email, password=password_one) 
      newUser.save() 
      return render_to_response('presentacion/login.html', context_instance=RequestContext(request)) 
     else: 
      ctx = {'form':form} 
      return render_to_response('presentacion/sign_up.html',ctx, context_instance=RequestContext(request)) 
    ctx = {'form':form} 
    return render_to_response('presentacion/sign_up.html',ctx, context_instance=RequestContext(request)) 

請幫忙!!!

+0

請張貼代碼爲你的'signup_view'方法。 – 2014-09-02 04:18:43

+0

問題出在你的'login.html'文件,而不是'sign_up.html'文件。在你的'login.html'文件中,你有'{{form | as_bootstrap}}'嗎? – 2014-09-02 04:27:01

+0

是的,我該怎麼辦? – user3117112 2014-09-02 04:31:34

回答

3

在你login.html模板,你有{{ form|as_bootstrap }},但在你簽署了代碼,你渲染登錄的模板,但你不及格的形式值:

return render_to_response('presentacion/login.html', 
          context_instance=RequestContext(request)) 

沒有上下文這裏。

您需要通過將用戶重定向到登錄視圖,而不是從您的註冊視圖顯示登錄模板來解決這個問題。

另外,您應該使用render shortcut,它會自動發送RequestContext

您也沒有檢查重複的用戶。

在代碼中解決這些問題:

from django.shortcuts import render, redirect 

def signup_view(request): 
    form = RegisterForm(request.POST or None) 
    ctx = {'form': form} 
    if request.method == "POST": 
     if form.is_valid(): 
      username = form.cleaned_data['username'] 
      name = form.cleaned_data['name'] 
      last_name = form.cleaned_data['last_name'] 
      email = form.cleaned_data['email'] 
      password_one = form.cleaned_data['password_one'] 
      password_two = form.cleaned_data['password_two'] 
      if not User.objects.filter(email=email).exists(): 
       newUser = User.objects.create_user(username=username, 
                first_name=name, 
                last_name=last_name, 
                email=email, 
                password=password_one) 
       newUser.save() 
      else: 
       # Do something, because a user 
       # with this email already exists 
       pass 
      return redirect('login') 

    return render(request, 'presentacion/sign_up.html', ctx) 
+0

非常感謝:D – user3117112 2014-09-02 05:55:39

相關問題