I’m trying to create a ModelForm to add info to a database from the view but the only thing displaying is the submit button. I tried using the documentation but I can’t tell why the form isn’t showing up. Here is what I have so far
forms.py
from django.forms import ModelForm
from .models import Edge
class EdgeForm(ModelForm):
class Meta:
model = Edge
exclude = ["user"]
views.py
@login_required(login_url="/login")
def def_edge(request):
form = EdgeForm
if form.is_valid:
return redirect("/meaiapp/dashboard")
else:
form = EdgeForm()
return render(request, "meai/meai_edge.html", {"form": form})
The page still shows the submit button only, I updated the code in views.py below. Are there errors in the forms.py too?
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from .forms import EdgeForm
# Create your views here.
@login_required(login_url="/login")
def def_edge(request):
if request.method == "POST":
form = EdgeForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("/meaiapp/dashboard")
else:
form = EdgeForm()
return render(request, "meai/meai_dashboard.html", {"form": form})
What does your Edge model look like? What does the “_app.html” template look like?
(I don’t see anything wrong here.)
Also, please confirm what directory that template is located in. (Do you possibly have a second copy of meai/meai_dashboard.html in a different location?)
Look at the html as it has been rendered in the browser to see if it’s in the page that has been sent. If it is, then you may have some css that is causing it to be obscured.
Also, in your original post you showed the template containing the {{ form.as_p }} reference but you didn’t specify which file that was in. Are you sure you’re rendering the right template here?
Ok. I believe I’m using the correct template if I understand what you are saying (unless you mean the {{form.as_p}} referencing ‘form’ from a specific file; I thought the form variable is limited in scope to the view being used). Also compared this html to the login/register forms which show the form fields and are being rendered with no issues. I’m not sure what it means but this is what the html for this form looks like. I’m only using bootstrap css at this point
Yes, I was asking you to confirm the file names involved. You’ve shown two different views referencing two different template names - and you’ve shown a fragment of a template rendering a form but without identifying the file name. So I was asking you to verify that you’re rendering the correct template.
Within the <form tag there is a div that can be expanded. Check that out to see if the form is present in that div. (You don’t need to post it here.)
If it’s just the button, then it’s very likely that you’re not rendering the template that you think you’re rendering. To check/verify this, make some other change to the template outside the form tag (add some text in an <h1> tag for example) and see if that is showing up on the page. If it isn’t then you’re not rendering the correct template.
Ok so the code inside the div tag was the button only, I added text in a h1 tag and it actually does show up on the page. If the correct template is being rendered, what else could be the issue? Removing the bootstrap didn’t fix it either
I did have a second view using the same template, changed the return statement to render a different template. I’m using a different template for each view now, still no change so I’ve added the print statement in the view.
updated view
@login_required(login_url="/login")
def def_edge(request):
if request.method == "POST":
form = EdgeForm(request.POST)
if form.is_valid():
form.save()
print(form.as_p())
return HttpResponseRedirect("/meaiapp/edge")
else:
form = EdgeForm()
return render(request, "meai/meai_edge.html", {"form": form})
else:
return render(request, "meai/meai_edge.html")
To clarify, when you say runserver console are you referring to the console in the browser? I’m not seeing anything there
Ok the form is working now, thanks really appreciate your help! One more question about the view, when I passed the form in the context I had to define form again. For the nested else statement, what would that typically handle? Or is it unnecessary to have there since the form validation is being addressed?
I’m sorry, I’m not sure I’m following what you’re trying to ask here.
In general, a function-based view will frequenty follow the pattern as described in the “view” section of the Working with Forms docs. (It would probably be worth your time to review the entire page. Now that you’ve got some more experience, you may understand more than you would have previously.)