Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,147,867 members, 7,798,894 topics. Date: Tuesday, 16 April 2024 at 11:55 AM

Django Developer Plz Help - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Django Developer Plz Help (1536 Views)

Python/django Developer / For Django Developer / Java Programmers Plz Help Out (2) (3) (4)

(1) (Reply) (Go Down)

Django Developer Plz Help by kensmoney(m): 1:54pm On Jul 20, 2019
Good guys!!
i am new in django, i am trying to save data to db but is not saving.
this is the my view.py

from django.shortcuts import render
from .models import Record
def Create(request):
data = Record()
if request.method == "POST":
data.name = request.POST.get("name" )
data.product = request.POST.get("product" )
data.price = request.POST.get("price" )
data.description = request.POST.get("description"wink
data.save()
return render(request, "html/home.html" )
THIS IS MY HTML FILE
<form class="myform" action="create/" method="POST">
<h1> Add Records </h1>
{% csrf_token %}
<div class="container">
<label role="name">name</label>
</div>
<div>
<input type="text" id="name" name="name">
</div>
<div class="container">
<label role="description">product</label>
</div>
<div>
<input type="text" id="product" name="product">
</div>
<div class="container">
<label role="price">price</label>
</div>
<div>
<input type="text" id="price" name="price">
</div>
<div class="container">
<label role="description">description</label>
</div>
<div>
<input type="text" id="description" name="description">
</div>
<div class="container">
<button type="submit" class="btn"> Save </button>
</div>

</form>
Re: Django Developer Plz Help by stanliwise(m): 3:39pm On Jul 20, 2019
kensmoney:
Good guys!!
i am new in django, i am trying to save data to db but is not saving.
this is the my view.py

from django.shortcuts import render
from .models import Record
def Create(request):
data = Record()
if request.method == "POST":
data.name = request.POST.get("name" )
data.product = request.POST.get("product" )
data.price = request.POST.get("price" )
data.description = request.POST.get("description"wink
data.save()
return render(request, "html/home.html" )
THIS IS MY HTML FILE
<form class="myform" action="create/" method="POST">
<h1> Add Records </h1>
{% csrf_token %}
<div class="container">
<label role="name">name</label>
</div>
<div>
<input type="text" id="name" name="name">
</div>
<div class="container">
<label role="description">product</label>
</div>
<div>
<input type="text" id="product" name="product">
</div>
<div class="container">
<label role="price">price</label>
</div>
<div>
<input type="text" id="price" name="price">
</div>
<div class="container">
<label role="description">description</label>
</div>
<div>
<input type="text" id="description" name="description">
</div>
<div class="container">
<button type="submit" class="btn"> Save </button>
</div>

</form>
Are you sure that your indentation are correct?

Also your
"import .models"
I thought it was
from django.db import models
without a "."
after that do this
data = models.Record()

If your controller was Create() function. Where did you call it?
Re: Django Developer Plz Help by kennedythacode: 4:20pm On Jul 20, 2019
instead of this
data.name = request.POST.get("name" )
do this
data.name = request.POST.["name" ]

remove the get from the rest

1 Like

Re: Django Developer Plz Help by Daejoyoung: 5:08pm On Jul 20, 2019
What of your models.py, that's usually the main thing when it comes to the database, also did you perform your migrations successfully?
Re: Django Developer Plz Help by Daejoyoung: 5:12pm On Jul 20, 2019
stanliwise:

Are you sure that your indentation are correct?

Also your
"import .models"
I thought it was
from django.db import models
without a "."
after that do this
data = models.Record()

If your controller was Create() function. Where did you call it?
You can also import anything from models into views, by using .models, because the models.py is already in same directory as views.py.
l think he needs to have a models.py file

1 Like

Re: Django Developer Plz Help by kensmoney(m): 5:24pm On Jul 20, 2019
Daejoyoung:
What of your models.py, that's usually the main thing when it comes to the database, also did you perform your migrations successfully?

this is my mode.py
class Record (models.Model):
id = models.IntegerField(default=0, primary_key=True)
name = models.CharField(max_length=100)
product = models.CharField(max_length=100)
price = models.CharField(max_length=100)
description = models.CharField(max_length=100)
Re: Django Developer Plz Help by Daejoyoung: 5:55pm On Jul 20, 2019
kensmoney:


this is my mode.py
class Records (models.Model):
id = models.IntegerField(default=0, primary_key=True)
name = models.CharField(max_length=100)
product = models.CharField(max_length=100)
price = models.CharField(max_length=100)
description = models.CharField(max_length=100)
What error does it give you exactly, can you screenshot it? Also the name of your class is Records, but in your views.py file, you wrote import Record. You missed the s in Records.
Re: Django Developer Plz Help by kensmoney(m): 5:59pm On Jul 20, 2019
[quote author=kennedythacode post=80460374] instead of this
data.name = request.POST.get("name" )
do this
data.name = request.POST.["name" ]

remove the get from the rest[/quote
Thanks. Let me try it
Re: Django Developer Plz Help by kensmoney(m): 6:03pm On Jul 20, 2019
Daejoyoung:

What error does it give you exactly, can you screenshot it? Also the name of your class is Records, but in your views.py file, you wrote import Record. You missed the s in Records.

No error. It's running fine but when I check my
db manage.py shell I will see empty dict

on my http status I see 200, but no commit message
Re: Django Developer Plz Help by Daejoyoung: 6:03pm On Jul 20, 2019
[quote author=kensmoney post=80463185][/quote]
Also change import Record in views .py, to import Records. Then data=Records()
Re: Django Developer Plz Help by Daejoyoung: 8:33pm On Jul 20, 2019
kensmoney:


No error. It's running fine but when I check my
db manage.py shell I will see empty dict

on my http status I see 200, but no commit message
ok then try to query the data from the ipython shell, so you could easily change the format in your view.py into a dictionary, something of this nature.....

data={"posts": Post.objects all()}

return render(request, "html/home.html" , data)
Re: Django Developer Plz Help by kensmoney(m): 9:46pm On Jul 22, 2019
Thank you all for your contributions. Problem solved........
.
Re: Django Developer Plz Help by Daejoyoung: 10:14pm On Jul 22, 2019
kensmoney:
Thank you all for your contributions. Problem solved........
.
Great, so how did you go about it?
Re: Django Developer Plz Help by bgberbo: 10:34am On Jul 27, 2019
Daejoyoung:

Great, so how did you go about it?
Daejoyoung:

Great, so how did you go about it?

Hello @ Daejoyoung. I have read through some of your responses here on nairaland, and I was hoping to reach you for a technical consultation. How could I reach you sir ?
Re: Django Developer Plz Help by Daejoyoung: 7:10am On Jul 29, 2019
bgberbo:


Hello @ Daejoyoung. I have read through some of your responses here on nairaland, and I was hoping to reach you for a technical consultation. How could I reach you sir ?
Hi sorry l tried to contact you, but l can't find your dm in my mail box, if it's nothing so personal, we could start the discussion here and then see how it goes.
l still have difficulties with this nairaland email shit.
Re: Django Developer Plz Help by bgberbo: 2:01pm On Jul 29, 2019
Daejoyoung:

Hi sorry l tried to contact you, but l can't find your dm in my mail box, if it's nothing so personal, we could start the discussion here and then see how it goes.
l still have difficulties with this nairaland email shit.

Oh! Sorry about not being able to reach me. Ok so we are floating some micro finance bank, and are looking at getting an independent person to look at the core application for us , for viability as we are looking at integrating with some ancillary programs for value addition. You can reach me on bgberbo@gmail.com

(1) (Reply)

Did You Go Into Coding Because Of Passion Or For The Money Or Both? / Which One Has More Prospect? Web Application/ Dev Or Oracle 11g / PIC Programmer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 50
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.