Cant render Objects and a Form in a CreateView class Django












-1














I'm simply trying to render objects and a form with a Create view class. Whenever my page loads it renders the objects or the form but not both at the same time. Is There any way of doing that? Heres my code. Hopefully, I provided enough information



urls.py -



 path('create/', AccountCreateView.as_view(), name='account-create'),


views.py -



class AccountCreateView(CreateView):
template_name = '../templates/account-create.html'
form_class = AccountModelForm
queryset = Account.objects.all()

def get(self, request, *args, **kwargs):
User = Profile.objects.get(user=request.user)
name = ExecutiveBook.objects.get(user=request.user)
accounts = Account.objects.filter(profile=name)
context = {'ac': accounts,
'my_list': accounts,
'user': User,}
return render(request, '../templates/account-create.html', context)


html template or file -



{% extends 'AccountBase.html' %}

{% load static %}

{% block content %}

<title>{{ user.user }} - Accuracy</title>

<link rel="stylesheet" type="text/css" href="{% static '/admin/css/profile.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static '/admin/css/bookkeep.css' %}" />

<div id="anav">
<div class="wrapper">
<div id="logo"><a href="/"><img src="{% static '/admin/img/AccuracyFbla2.png' %}" /> <h2>| Account</h2></a></div>
<div id="profileicon"><img src="../static/admin/img/default-profile-picture-3.jpg"/></div>
<div id="cog"><img src="{% static '/admin/img/if_Settings_1891011.png' %}"/></div>
<div id="bell"><img src="{% static '/admin/img/notifications_white_192x192.png' %}"/></div>
</div>

<ul id="apptabs">
<a href="/profile"><li>Dashboard</li></a>
<a href="/bookkeeping"><li style="background-color: white; color: black;">Bookkeeping</li></a>
<a href=""><li>Annual Report</li></a>
<a href=""><li>Internal Audit</li></a>
<a href=""><li>Schedule Appointments</li></a>
</ul>
</div>

<div id="subnav">
<h1>Accounts</h1>
<h3>Welcome Back, {{ user.user }}</h3>
</div>

<form action="." method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>

{% endblock %}


models.py (if needed) -



class Account (models.Model):
name = models.TextField(max_length=100, null=True)
balance = models.DecimalField(max_digits= 20, decimal_places=2, null=True)
profile = models.ForeignKey(ExecutiveBook, on_delete=models.CASCADE)

def __str__(self):
return self.name

def get_absolute_url(self):
return '/bookkeeping'


And The Page:



enter image description here










share|improve this question




















  • 1




    {{ form.as.p }} should instead be {{ form.as_p }}. Also, don't post your code as images -- it's much harder for people to help you.
    – John Gordon
    Nov 23 at 4:30












  • thank you, good to know. It works when I remove def get() but when I try to load my objects the form still doesn't show up. Thank you though, I did learn something so your time wasn't wasted.
    – McDiamund
    Nov 23 at 4:35






  • 1




    The context dictionary in get() doesn't contain a form object, therefore the template can't render anything named {{ form }}.
    – John Gordon
    Nov 23 at 4:37












  • That makes a lot of sense. So that means I can't load any outside objects using get() in my createView? Is there any other way of doing so or is that just final? I am new to Django so I don't have the experience like you have yourself.
    – McDiamund
    Nov 23 at 4:45






  • 1




    The usual practice is to create a blank form in get() and pass that to the template.
    – John Gordon
    Nov 23 at 4:48
















-1














I'm simply trying to render objects and a form with a Create view class. Whenever my page loads it renders the objects or the form but not both at the same time. Is There any way of doing that? Heres my code. Hopefully, I provided enough information



urls.py -



 path('create/', AccountCreateView.as_view(), name='account-create'),


views.py -



class AccountCreateView(CreateView):
template_name = '../templates/account-create.html'
form_class = AccountModelForm
queryset = Account.objects.all()

def get(self, request, *args, **kwargs):
User = Profile.objects.get(user=request.user)
name = ExecutiveBook.objects.get(user=request.user)
accounts = Account.objects.filter(profile=name)
context = {'ac': accounts,
'my_list': accounts,
'user': User,}
return render(request, '../templates/account-create.html', context)


html template or file -



{% extends 'AccountBase.html' %}

{% load static %}

{% block content %}

<title>{{ user.user }} - Accuracy</title>

<link rel="stylesheet" type="text/css" href="{% static '/admin/css/profile.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static '/admin/css/bookkeep.css' %}" />

<div id="anav">
<div class="wrapper">
<div id="logo"><a href="/"><img src="{% static '/admin/img/AccuracyFbla2.png' %}" /> <h2>| Account</h2></a></div>
<div id="profileicon"><img src="../static/admin/img/default-profile-picture-3.jpg"/></div>
<div id="cog"><img src="{% static '/admin/img/if_Settings_1891011.png' %}"/></div>
<div id="bell"><img src="{% static '/admin/img/notifications_white_192x192.png' %}"/></div>
</div>

<ul id="apptabs">
<a href="/profile"><li>Dashboard</li></a>
<a href="/bookkeeping"><li style="background-color: white; color: black;">Bookkeeping</li></a>
<a href=""><li>Annual Report</li></a>
<a href=""><li>Internal Audit</li></a>
<a href=""><li>Schedule Appointments</li></a>
</ul>
</div>

<div id="subnav">
<h1>Accounts</h1>
<h3>Welcome Back, {{ user.user }}</h3>
</div>

<form action="." method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>

{% endblock %}


models.py (if needed) -



class Account (models.Model):
name = models.TextField(max_length=100, null=True)
balance = models.DecimalField(max_digits= 20, decimal_places=2, null=True)
profile = models.ForeignKey(ExecutiveBook, on_delete=models.CASCADE)

def __str__(self):
return self.name

def get_absolute_url(self):
return '/bookkeeping'


And The Page:



enter image description here










share|improve this question




















  • 1




    {{ form.as.p }} should instead be {{ form.as_p }}. Also, don't post your code as images -- it's much harder for people to help you.
    – John Gordon
    Nov 23 at 4:30












  • thank you, good to know. It works when I remove def get() but when I try to load my objects the form still doesn't show up. Thank you though, I did learn something so your time wasn't wasted.
    – McDiamund
    Nov 23 at 4:35






  • 1




    The context dictionary in get() doesn't contain a form object, therefore the template can't render anything named {{ form }}.
    – John Gordon
    Nov 23 at 4:37












  • That makes a lot of sense. So that means I can't load any outside objects using get() in my createView? Is there any other way of doing so or is that just final? I am new to Django so I don't have the experience like you have yourself.
    – McDiamund
    Nov 23 at 4:45






  • 1




    The usual practice is to create a blank form in get() and pass that to the template.
    – John Gordon
    Nov 23 at 4:48














-1












-1








-1


0





I'm simply trying to render objects and a form with a Create view class. Whenever my page loads it renders the objects or the form but not both at the same time. Is There any way of doing that? Heres my code. Hopefully, I provided enough information



urls.py -



 path('create/', AccountCreateView.as_view(), name='account-create'),


views.py -



class AccountCreateView(CreateView):
template_name = '../templates/account-create.html'
form_class = AccountModelForm
queryset = Account.objects.all()

def get(self, request, *args, **kwargs):
User = Profile.objects.get(user=request.user)
name = ExecutiveBook.objects.get(user=request.user)
accounts = Account.objects.filter(profile=name)
context = {'ac': accounts,
'my_list': accounts,
'user': User,}
return render(request, '../templates/account-create.html', context)


html template or file -



{% extends 'AccountBase.html' %}

{% load static %}

{% block content %}

<title>{{ user.user }} - Accuracy</title>

<link rel="stylesheet" type="text/css" href="{% static '/admin/css/profile.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static '/admin/css/bookkeep.css' %}" />

<div id="anav">
<div class="wrapper">
<div id="logo"><a href="/"><img src="{% static '/admin/img/AccuracyFbla2.png' %}" /> <h2>| Account</h2></a></div>
<div id="profileicon"><img src="../static/admin/img/default-profile-picture-3.jpg"/></div>
<div id="cog"><img src="{% static '/admin/img/if_Settings_1891011.png' %}"/></div>
<div id="bell"><img src="{% static '/admin/img/notifications_white_192x192.png' %}"/></div>
</div>

<ul id="apptabs">
<a href="/profile"><li>Dashboard</li></a>
<a href="/bookkeeping"><li style="background-color: white; color: black;">Bookkeeping</li></a>
<a href=""><li>Annual Report</li></a>
<a href=""><li>Internal Audit</li></a>
<a href=""><li>Schedule Appointments</li></a>
</ul>
</div>

<div id="subnav">
<h1>Accounts</h1>
<h3>Welcome Back, {{ user.user }}</h3>
</div>

<form action="." method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>

{% endblock %}


models.py (if needed) -



class Account (models.Model):
name = models.TextField(max_length=100, null=True)
balance = models.DecimalField(max_digits= 20, decimal_places=2, null=True)
profile = models.ForeignKey(ExecutiveBook, on_delete=models.CASCADE)

def __str__(self):
return self.name

def get_absolute_url(self):
return '/bookkeeping'


And The Page:



enter image description here










share|improve this question















I'm simply trying to render objects and a form with a Create view class. Whenever my page loads it renders the objects or the form but not both at the same time. Is There any way of doing that? Heres my code. Hopefully, I provided enough information



urls.py -



 path('create/', AccountCreateView.as_view(), name='account-create'),


views.py -



class AccountCreateView(CreateView):
template_name = '../templates/account-create.html'
form_class = AccountModelForm
queryset = Account.objects.all()

def get(self, request, *args, **kwargs):
User = Profile.objects.get(user=request.user)
name = ExecutiveBook.objects.get(user=request.user)
accounts = Account.objects.filter(profile=name)
context = {'ac': accounts,
'my_list': accounts,
'user': User,}
return render(request, '../templates/account-create.html', context)


html template or file -



{% extends 'AccountBase.html' %}

{% load static %}

{% block content %}

<title>{{ user.user }} - Accuracy</title>

<link rel="stylesheet" type="text/css" href="{% static '/admin/css/profile.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static '/admin/css/bookkeep.css' %}" />

<div id="anav">
<div class="wrapper">
<div id="logo"><a href="/"><img src="{% static '/admin/img/AccuracyFbla2.png' %}" /> <h2>| Account</h2></a></div>
<div id="profileicon"><img src="../static/admin/img/default-profile-picture-3.jpg"/></div>
<div id="cog"><img src="{% static '/admin/img/if_Settings_1891011.png' %}"/></div>
<div id="bell"><img src="{% static '/admin/img/notifications_white_192x192.png' %}"/></div>
</div>

<ul id="apptabs">
<a href="/profile"><li>Dashboard</li></a>
<a href="/bookkeeping"><li style="background-color: white; color: black;">Bookkeeping</li></a>
<a href=""><li>Annual Report</li></a>
<a href=""><li>Internal Audit</li></a>
<a href=""><li>Schedule Appointments</li></a>
</ul>
</div>

<div id="subnav">
<h1>Accounts</h1>
<h3>Welcome Back, {{ user.user }}</h3>
</div>

<form action="." method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>

{% endblock %}


models.py (if needed) -



class Account (models.Model):
name = models.TextField(max_length=100, null=True)
balance = models.DecimalField(max_digits= 20, decimal_places=2, null=True)
profile = models.ForeignKey(ExecutiveBook, on_delete=models.CASCADE)

def __str__(self):
return self.name

def get_absolute_url(self):
return '/bookkeeping'


And The Page:



enter image description here







python django django-forms django-views






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 4:40

























asked Nov 23 at 4:24









McDiamund

205




205








  • 1




    {{ form.as.p }} should instead be {{ form.as_p }}. Also, don't post your code as images -- it's much harder for people to help you.
    – John Gordon
    Nov 23 at 4:30












  • thank you, good to know. It works when I remove def get() but when I try to load my objects the form still doesn't show up. Thank you though, I did learn something so your time wasn't wasted.
    – McDiamund
    Nov 23 at 4:35






  • 1




    The context dictionary in get() doesn't contain a form object, therefore the template can't render anything named {{ form }}.
    – John Gordon
    Nov 23 at 4:37












  • That makes a lot of sense. So that means I can't load any outside objects using get() in my createView? Is there any other way of doing so or is that just final? I am new to Django so I don't have the experience like you have yourself.
    – McDiamund
    Nov 23 at 4:45






  • 1




    The usual practice is to create a blank form in get() and pass that to the template.
    – John Gordon
    Nov 23 at 4:48














  • 1




    {{ form.as.p }} should instead be {{ form.as_p }}. Also, don't post your code as images -- it's much harder for people to help you.
    – John Gordon
    Nov 23 at 4:30












  • thank you, good to know. It works when I remove def get() but when I try to load my objects the form still doesn't show up. Thank you though, I did learn something so your time wasn't wasted.
    – McDiamund
    Nov 23 at 4:35






  • 1




    The context dictionary in get() doesn't contain a form object, therefore the template can't render anything named {{ form }}.
    – John Gordon
    Nov 23 at 4:37












  • That makes a lot of sense. So that means I can't load any outside objects using get() in my createView? Is there any other way of doing so or is that just final? I am new to Django so I don't have the experience like you have yourself.
    – McDiamund
    Nov 23 at 4:45






  • 1




    The usual practice is to create a blank form in get() and pass that to the template.
    – John Gordon
    Nov 23 at 4:48








1




1




{{ form.as.p }} should instead be {{ form.as_p }}. Also, don't post your code as images -- it's much harder for people to help you.
– John Gordon
Nov 23 at 4:30






{{ form.as.p }} should instead be {{ form.as_p }}. Also, don't post your code as images -- it's much harder for people to help you.
– John Gordon
Nov 23 at 4:30














thank you, good to know. It works when I remove def get() but when I try to load my objects the form still doesn't show up. Thank you though, I did learn something so your time wasn't wasted.
– McDiamund
Nov 23 at 4:35




thank you, good to know. It works when I remove def get() but when I try to load my objects the form still doesn't show up. Thank you though, I did learn something so your time wasn't wasted.
– McDiamund
Nov 23 at 4:35




1




1




The context dictionary in get() doesn't contain a form object, therefore the template can't render anything named {{ form }}.
– John Gordon
Nov 23 at 4:37






The context dictionary in get() doesn't contain a form object, therefore the template can't render anything named {{ form }}.
– John Gordon
Nov 23 at 4:37














That makes a lot of sense. So that means I can't load any outside objects using get() in my createView? Is there any other way of doing so or is that just final? I am new to Django so I don't have the experience like you have yourself.
– McDiamund
Nov 23 at 4:45




That makes a lot of sense. So that means I can't load any outside objects using get() in my createView? Is there any other way of doing so or is that just final? I am new to Django so I don't have the experience like you have yourself.
– McDiamund
Nov 23 at 4:45




1




1




The usual practice is to create a blank form in get() and pass that to the template.
– John Gordon
Nov 23 at 4:48




The usual practice is to create a blank form in get() and pass that to the template.
– John Gordon
Nov 23 at 4:48

















active

oldest

votes











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%2f53440637%2fcant-render-objects-and-a-form-in-a-createview-class-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53440637%2fcant-render-objects-and-a-form-in-a-createview-class-django%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

Lallio

Futebolista

Jornalista