Reputation: 1
This is the code i have written-
this is my html page
**<home.html>**
<!-- New hardware Button -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
New Hardware
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Enter Hardware Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="" method="post" id= "dataform">
{% csrf_token %}
{{ form.as_table }}
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" form="dataform">Save changes</button>
</div>
</div>
</div>
</div>
This is my views
**<views.py>**
def home(request):
obj = hardware.objects.get(id=1)
Display = [{
'productgroup' : obj.productgroup,
'make' : obj.make,
'model' : obj.model,
'serial' : obj.serial,
'value' : obj.Value,
'purchasedate' : obj.purchasedate,
'bookvalue' : obj.bookvalue,
'solid' : obj.solid
}]
return render (request, "home.html", {'Display': Display} )
def newentry_view(request):
form = newentry()
print (form.fields)
if request.method == "POST":
form = newentry(request.POST)
if form.is_valid():
form.save()
else:
form = newentry()
content = {'form' : form}
return render (request, "home.html", content)
This is the forms created
**<forms.py>**
from django import forms
from .models import hardware
class newentry (forms.ModelForm):
class Meta:
model = hardware
fields = "__all__"
This is my model
**<Models.py>**
from django.db import models
class hardware (models.Model):
productgroup = models.CharField(max_length=50)
make = models.CharField(max_length=50)
model = models.CharField(max_length=40)
serial = models.CharField(max_length=40)
Value = models.DecimalField(decimal_places=2, max_digits=15)
purchasedate = models.DateField()
bookvalue = models.DecimalField(decimal_places=2, max_digits=15)
solid = models.IntegerField()
<urls.py>
from django.contrib import admin
from django.urls import path
from newhwd.views import home
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', home)
]
Trying to create a Basic CRUD in django.I want a Modal to display the fields to enter a new hardware detail. But only a blank Modal is appearing. I'm not getting any error as well. My retrieving data from backend is working fine. Only the form fields are not displaying. Changed the "content" in template to form.
Upvotes: 0
Views: 31
Reputation: 71
In your template replace {{ content.as_table }}
with {{ form.as_table }}
.
It's custom to call content context instead.
As a side note - you seem to have the context quite overcomplicated in your home view:
def home(request):
obj = hardware.objects.get(id=1)
context = {
'object' : object,
}
return render (request, "home.html", context )
This way you can access each of these keys from your dictionary in your template {{ object.serial }}
or {{ object.bookvalue }}
Upvotes: 0