AttributeError:'str'对象没有属性'fields'使用Django non rel on GAE

时间:2014-09-02 04:14:33

标签: python django twitter-bootstrap google-app-engine django-nonrel

我正在使用Django非常依赖Google App Engine进行应用程序,我正在使用Bootstrap ...在Django Forms上使用Bootstrap我安装了django_forms_bootstrap(https://github.com/pinax/django-forms-bootstrap

事情是......当我在GAE上部署并尝试创建新用户(注册)时,我得到服务器错误(500),但新用户被创建... GAE的日志显示:< / p>

> 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

我的singup_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))

请帮助!!!

2 个答案:

答案 0 :(得分:4)

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)

答案 1 :(得分:0)

如果像我一样在这里遇到类似的错误,在Django 2.2.4中,使用as_table|crispy似乎根本不起作用。解决方法是删除as_table

之前

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

之后

<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>

这可能无法回答OP的问题,但是如果您像我一样来到这里,希望对您有所帮助。