Django page not saving data correctly
I have a form in a HTML page that I'm using for a Django project. This form takes the input from the user and sends it to a page which should save it to the database, but right now its not doing do. Here is the code:
<!DOCTYPE html>
<html>
<body>
<h2>Create product here</h2>
<div>
<form id="new_user_form" method="post" action="user/create"}>
% csrf_token %
<div>
<label for="name" > Name:<br></label>
<input type="text" id="name"/>
</div>
<br/>
<div>
<label for="description"> description:<br></label>
<input type="text" id="description"/>
</div>
<div>
<label for="price" > price:<br></label>
<input type="text" id="price"/>
</div>
<div>
<input type="submit" value="submit"/>
</div>
</div>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</html>
My urls.py file:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from testapp import views
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('user/create', views.create_user, name='create_user')
]
The views.py file:
from django.shortcuts import render
from testapp.models import User
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
def create_user(request):
if request.method == 'POST':
name = request.POST.get('name')
description = request.POST.get('description')
price = request.POST.get('price')
newUser = User(
name = name,
description = description,
price = price
)
newUser.save()
return HttpResponse('')
And the models.py file:
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length = 32, null = True)
description = models.TextField(null = True)
price = models.CharField(max_length = 128, null = True)
Now the problem is when the form data gets sent to the function create_user, it should take the data and create a object with that data and save it to the database. The database is set up correctly as when I use the Django shell to test it, users are created and saved. However, through the form and the python, something is going wrong here and I'm not sure why. Can someone help me out here?
python django post request
add a comment |
I have a form in a HTML page that I'm using for a Django project. This form takes the input from the user and sends it to a page which should save it to the database, but right now its not doing do. Here is the code:
<!DOCTYPE html>
<html>
<body>
<h2>Create product here</h2>
<div>
<form id="new_user_form" method="post" action="user/create"}>
% csrf_token %
<div>
<label for="name" > Name:<br></label>
<input type="text" id="name"/>
</div>
<br/>
<div>
<label for="description"> description:<br></label>
<input type="text" id="description"/>
</div>
<div>
<label for="price" > price:<br></label>
<input type="text" id="price"/>
</div>
<div>
<input type="submit" value="submit"/>
</div>
</div>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</html>
My urls.py file:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from testapp import views
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('user/create', views.create_user, name='create_user')
]
The views.py file:
from django.shortcuts import render
from testapp.models import User
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
def create_user(request):
if request.method == 'POST':
name = request.POST.get('name')
description = request.POST.get('description')
price = request.POST.get('price')
newUser = User(
name = name,
description = description,
price = price
)
newUser.save()
return HttpResponse('')
And the models.py file:
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length = 32, null = True)
description = models.TextField(null = True)
price = models.CharField(max_length = 128, null = True)
Now the problem is when the form data gets sent to the function create_user, it should take the data and create a object with that data and save it to the database. The database is set up correctly as when I use the Django shell to test it, users are created and saved. However, through the form and the python, something is going wrong here and I'm not sure why. Can someone help me out here?
python django post request
I'd recommend that you take a look at Django documentation regarding forms. This will give you a better way of approaching your use case.
– ivissani
Nov 13 '18 at 15:45
add a comment |
I have a form in a HTML page that I'm using for a Django project. This form takes the input from the user and sends it to a page which should save it to the database, but right now its not doing do. Here is the code:
<!DOCTYPE html>
<html>
<body>
<h2>Create product here</h2>
<div>
<form id="new_user_form" method="post" action="user/create"}>
% csrf_token %
<div>
<label for="name" > Name:<br></label>
<input type="text" id="name"/>
</div>
<br/>
<div>
<label for="description"> description:<br></label>
<input type="text" id="description"/>
</div>
<div>
<label for="price" > price:<br></label>
<input type="text" id="price"/>
</div>
<div>
<input type="submit" value="submit"/>
</div>
</div>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</html>
My urls.py file:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from testapp import views
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('user/create', views.create_user, name='create_user')
]
The views.py file:
from django.shortcuts import render
from testapp.models import User
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
def create_user(request):
if request.method == 'POST':
name = request.POST.get('name')
description = request.POST.get('description')
price = request.POST.get('price')
newUser = User(
name = name,
description = description,
price = price
)
newUser.save()
return HttpResponse('')
And the models.py file:
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length = 32, null = True)
description = models.TextField(null = True)
price = models.CharField(max_length = 128, null = True)
Now the problem is when the form data gets sent to the function create_user, it should take the data and create a object with that data and save it to the database. The database is set up correctly as when I use the Django shell to test it, users are created and saved. However, through the form and the python, something is going wrong here and I'm not sure why. Can someone help me out here?
python django post request
I have a form in a HTML page that I'm using for a Django project. This form takes the input from the user and sends it to a page which should save it to the database, but right now its not doing do. Here is the code:
<!DOCTYPE html>
<html>
<body>
<h2>Create product here</h2>
<div>
<form id="new_user_form" method="post" action="user/create"}>
% csrf_token %
<div>
<label for="name" > Name:<br></label>
<input type="text" id="name"/>
</div>
<br/>
<div>
<label for="description"> description:<br></label>
<input type="text" id="description"/>
</div>
<div>
<label for="price" > price:<br></label>
<input type="text" id="price"/>
</div>
<div>
<input type="submit" value="submit"/>
</div>
</div>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</html>
My urls.py file:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from testapp import views
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('user/create', views.create_user, name='create_user')
]
The views.py file:
from django.shortcuts import render
from testapp.models import User
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
def create_user(request):
if request.method == 'POST':
name = request.POST.get('name')
description = request.POST.get('description')
price = request.POST.get('price')
newUser = User(
name = name,
description = description,
price = price
)
newUser.save()
return HttpResponse('')
And the models.py file:
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length = 32, null = True)
description = models.TextField(null = True)
price = models.CharField(max_length = 128, null = True)
Now the problem is when the form data gets sent to the function create_user, it should take the data and create a object with that data and save it to the database. The database is set up correctly as when I use the Django shell to test it, users are created and saved. However, through the form and the python, something is going wrong here and I'm not sure why. Can someone help me out here?
python django post request
python django post request
asked Nov 13 '18 at 15:27
ArsenalfanArsenalfan
135
135
I'd recommend that you take a look at Django documentation regarding forms. This will give you a better way of approaching your use case.
– ivissani
Nov 13 '18 at 15:45
add a comment |
I'd recommend that you take a look at Django documentation regarding forms. This will give you a better way of approaching your use case.
– ivissani
Nov 13 '18 at 15:45
I'd recommend that you take a look at Django documentation regarding forms. This will give you a better way of approaching your use case.
– ivissani
Nov 13 '18 at 15:45
I'd recommend that you take a look at Django documentation regarding forms. This will give you a better way of approaching your use case.
– ivissani
Nov 13 '18 at 15:45
add a comment |
1 Answer
1
active
oldest
votes
You need to put the name in the inputs html tags
<input type="text" id="name" name="name"/>
<input type="text" id="description" name="description"/>
<input type="text" id="price" name="price"/>
Changed it to this, didnt fix the problem.
– Arsenalfan
Nov 13 '18 at 16:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284288%2fdjango-page-not-saving-data-correctly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to put the name in the inputs html tags
<input type="text" id="name" name="name"/>
<input type="text" id="description" name="description"/>
<input type="text" id="price" name="price"/>
Changed it to this, didnt fix the problem.
– Arsenalfan
Nov 13 '18 at 16:23
add a comment |
You need to put the name in the inputs html tags
<input type="text" id="name" name="name"/>
<input type="text" id="description" name="description"/>
<input type="text" id="price" name="price"/>
Changed it to this, didnt fix the problem.
– Arsenalfan
Nov 13 '18 at 16:23
add a comment |
You need to put the name in the inputs html tags
<input type="text" id="name" name="name"/>
<input type="text" id="description" name="description"/>
<input type="text" id="price" name="price"/>
You need to put the name in the inputs html tags
<input type="text" id="name" name="name"/>
<input type="text" id="description" name="description"/>
<input type="text" id="price" name="price"/>
answered Nov 13 '18 at 15:41
grouchoboygrouchoboy
46228
46228
Changed it to this, didnt fix the problem.
– Arsenalfan
Nov 13 '18 at 16:23
add a comment |
Changed it to this, didnt fix the problem.
– Arsenalfan
Nov 13 '18 at 16:23
Changed it to this, didnt fix the problem.
– Arsenalfan
Nov 13 '18 at 16:23
Changed it to this, didnt fix the problem.
– Arsenalfan
Nov 13 '18 at 16:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284288%2fdjango-page-not-saving-data-correctly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'd recommend that you take a look at Django documentation regarding forms. This will give you a better way of approaching your use case.
– ivissani
Nov 13 '18 at 15:45