简体   繁体   English

'signup'对象没有属性'get'

[英]'signup' object has no attribute 'get'

I want to store a username through User input but I am getting the following error: 我想通过用户输入存储用户名,但是出现以下错误:

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

Here is the relevant code I am trying to run: 这是我要运行的相关代码:

models.py : models.py

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

    def __unicode__ (self):
        return self.username

views.py 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 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 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>

error that I am getting on opening that page 我在打开该页面时遇到的错误

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

There's an error in your urls.py file when you match the ^sign/ pattern with the signup view but in your views you've only defined only the view sign . 当您将^sign/模式与signup视图匹配时, urls.py文件中会出现错误,但是在您的视图中,您仅定义了view sign

The error you are referring is because in your sign view you only deal with POST requests, therefore there is not way to deal with GET requests. 您所指的错误是因为在符号视图中您仅处理POST请求,因此无法处理GET请求。 May you can change as follows: 可以更改如下:

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

where bookstore/login.html could be a simple HTML form with username field and in submission post to ^sign/ . 其中bookstore/login.html可以是带有用户名字段且提交到^sign/的简单HTML表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM