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 :
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'),
)
<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']
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
.
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. 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/
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.