Django page not saving data correctly










0















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?










share|improve this question






















  • 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















0















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?










share|improve this question






















  • 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













0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer























  • Changed it to this, didnt fix the problem.

    – Arsenalfan
    Nov 13 '18 at 16:23










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



);













draft saved

draft discarded


















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









0














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





share|improve this answer























  • Changed it to this, didnt fix the problem.

    – Arsenalfan
    Nov 13 '18 at 16:23















0














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





share|improve this answer























  • Changed it to this, didnt fix the problem.

    – Arsenalfan
    Nov 13 '18 at 16:23













0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党