gpt4 book ai didi

python - “signup”对象没有属性 'get'

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:30 29 4
gpt4 key购买 nike

我想通过用户输入存储用户名,但收到以下错误:

`AttributeError` at /sign/ `'signup' object has no attribute 'get'`

这是我尝试运行的相关代码:

models.py:

class signup(models.Model):
username = models.CharField(max_length = 50)

def __unicode__ (self):
return self.username

views.py

def sign(request):
username = request.POST['username']
user = signup.objects.create(username=username)
return render(request,'bookstore/signup.html',{'username': username})

bookstore/urls.py

from django.conf.urls import patterns, url
from bookstore import views
urlpatterns = patterns(' ',
url(r'^$',views.home, name = 'home'),
url(r'^sign/$', views.signup,name='signup'),
url(r'^bookshow/$',views.bookshow,name='bookshow'),
url(r'^bookshow/(?P<book_id>\d+)/$',views.bookdetails, name='bookdetails'),
)

signup.html

 <html>


<h2>WELCOME TO BOOKSTORE<h2>
<h3>LOGIN TO GET IN LIBRARAY<h3>



{% if errors %}
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}


<div>
<form action="/sign/" method = "POST">
{% csrf_token %}

<input type="text" username="username" placeholder="USERNAME"/><br />
<input type="submit" value = "signup"/>
</form>
</div>

</html>

我打开该页面时遇到错误

 AttributeError at /sign/
'signup' object has no attribute 'get'
Request Method: GET
Request URL:
Django Version: 1.6.2
Exception Type: AttributeError
Exception Value:
'signup' object has no attribute 'get'
Exception Location: /usr/local/lib/python2.7/dist- packages/django/middleware/clickjacking.py in process_response, line 30
Python Executable: /usr/bin/python
Python Version: 2.7.5
Python Path:
['/home/prakhar/pp/django/mysit',
'/usr/local/lib/python2.7/dist-packages/pylint-1.1.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/astroid-1.0.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/logilab_common-0.61.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/pep8-1.5.3-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']

最佳答案

当您将 ^sign/ 模式与 signup View 匹配时,您的 urls.py 文件中出现错误,但在您的 View 中只定义了 View sign

您提到的错误是因为在您的签名 View 中您只处理 POST 请求,因此无法处理 GET 请求。您可以更改如下:

def sign(request):
if request.POST:
username = request.POST['username']
user = signup.objects.create(username=username)
return render(request,'bookstore/signup.html',{'username': username})
return render(request, 'bookstore/login.html')

其中 bookstore/login.html 可以是一个带有用户名字段的简单 HTML 表单,并提交到 ^sign/

关于python - “signup”对象没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23085174/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com