Django view with form and objects
I want to create view with form to create post and show post in the same page
but i don't know how i can do it because when i add to view.py form i dont see my objects i mean "posts from database"
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.http import Http404
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import HomeForm
from .models import simplePost
class IndexView(generic.ListView):
template_name = 'myapp/index.html'
def get_queryset(self):
return simplePost.objects.all()
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = simplePost
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['message'] = simplePost.objects.all() # filter this?
return context
python django
add a comment |
I want to create view with form to create post and show post in the same page
but i don't know how i can do it because when i add to view.py form i dont see my objects i mean "posts from database"
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.http import Http404
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import HomeForm
from .models import simplePost
class IndexView(generic.ListView):
template_name = 'myapp/index.html'
def get_queryset(self):
return simplePost.objects.all()
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = simplePost
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['message'] = simplePost.objects.all() # filter this?
return context
python django
new_item = get_object_or_404(simplePost)
likely fails if there are nosimplePost
objects, or more than one.
– Willem Van Onsem
Nov 13 '18 at 22:20
With new_item = get_object_or_404(simplePost) is my bad because i try repair this and i paste wrong code sorry
– Muefi
Nov 13 '18 at 22:25
I think it is probably better to use aFormView
here, since this will already reduce the view a lot, hence it is easier to spot mistakes in the remaining parts: docs.djangoproject.com/en/2.1/ref/class-based-views/…
– Willem Van Onsem
Nov 13 '18 at 22:26
do you want to show saved simplePost object after it will be created?
– Alexander Tyapkov
Nov 13 '18 at 22:28
Hmm yes i used FormView i see my form in template but i dont see objects i mean posts from database only when i delete all functions associated with form i see my posts
– Muefi
Nov 13 '18 at 22:31
add a comment |
I want to create view with form to create post and show post in the same page
but i don't know how i can do it because when i add to view.py form i dont see my objects i mean "posts from database"
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.http import Http404
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import HomeForm
from .models import simplePost
class IndexView(generic.ListView):
template_name = 'myapp/index.html'
def get_queryset(self):
return simplePost.objects.all()
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = simplePost
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['message'] = simplePost.objects.all() # filter this?
return context
python django
I want to create view with form to create post and show post in the same page
but i don't know how i can do it because when i add to view.py form i dont see my objects i mean "posts from database"
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.http import Http404
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import HomeForm
from .models import simplePost
class IndexView(generic.ListView):
template_name = 'myapp/index.html'
def get_queryset(self):
return simplePost.objects.all()
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = simplePost
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['message'] = simplePost.objects.all() # filter this?
return context
python django
python django
edited Nov 13 '18 at 22:46
Muefi
asked Nov 13 '18 at 22:18
MuefiMuefi
11
11
new_item = get_object_or_404(simplePost)
likely fails if there are nosimplePost
objects, or more than one.
– Willem Van Onsem
Nov 13 '18 at 22:20
With new_item = get_object_or_404(simplePost) is my bad because i try repair this and i paste wrong code sorry
– Muefi
Nov 13 '18 at 22:25
I think it is probably better to use aFormView
here, since this will already reduce the view a lot, hence it is easier to spot mistakes in the remaining parts: docs.djangoproject.com/en/2.1/ref/class-based-views/…
– Willem Van Onsem
Nov 13 '18 at 22:26
do you want to show saved simplePost object after it will be created?
– Alexander Tyapkov
Nov 13 '18 at 22:28
Hmm yes i used FormView i see my form in template but i dont see objects i mean posts from database only when i delete all functions associated with form i see my posts
– Muefi
Nov 13 '18 at 22:31
add a comment |
new_item = get_object_or_404(simplePost)
likely fails if there are nosimplePost
objects, or more than one.
– Willem Van Onsem
Nov 13 '18 at 22:20
With new_item = get_object_or_404(simplePost) is my bad because i try repair this and i paste wrong code sorry
– Muefi
Nov 13 '18 at 22:25
I think it is probably better to use aFormView
here, since this will already reduce the view a lot, hence it is easier to spot mistakes in the remaining parts: docs.djangoproject.com/en/2.1/ref/class-based-views/…
– Willem Van Onsem
Nov 13 '18 at 22:26
do you want to show saved simplePost object after it will be created?
– Alexander Tyapkov
Nov 13 '18 at 22:28
Hmm yes i used FormView i see my form in template but i dont see objects i mean posts from database only when i delete all functions associated with form i see my posts
– Muefi
Nov 13 '18 at 22:31
new_item = get_object_or_404(simplePost)
likely fails if there are no simplePost
objects, or more than one.– Willem Van Onsem
Nov 13 '18 at 22:20
new_item = get_object_or_404(simplePost)
likely fails if there are no simplePost
objects, or more than one.– Willem Van Onsem
Nov 13 '18 at 22:20
With new_item = get_object_or_404(simplePost) is my bad because i try repair this and i paste wrong code sorry
– Muefi
Nov 13 '18 at 22:25
With new_item = get_object_or_404(simplePost) is my bad because i try repair this and i paste wrong code sorry
– Muefi
Nov 13 '18 at 22:25
I think it is probably better to use a
FormView
here, since this will already reduce the view a lot, hence it is easier to spot mistakes in the remaining parts: docs.djangoproject.com/en/2.1/ref/class-based-views/…– Willem Van Onsem
Nov 13 '18 at 22:26
I think it is probably better to use a
FormView
here, since this will already reduce the view a lot, hence it is easier to spot mistakes in the remaining parts: docs.djangoproject.com/en/2.1/ref/class-based-views/…– Willem Van Onsem
Nov 13 '18 at 22:26
do you want to show saved simplePost object after it will be created?
– Alexander Tyapkov
Nov 13 '18 at 22:28
do you want to show saved simplePost object after it will be created?
– Alexander Tyapkov
Nov 13 '18 at 22:28
Hmm yes i used FormView i see my form in template but i dont see objects i mean posts from database only when i delete all functions associated with form i see my posts
– Muefi
Nov 13 '18 at 22:31
Hmm yes i used FormView i see my form in template but i dont see objects i mean posts from database only when i delete all functions associated with form i see my posts
– Muefi
Nov 13 '18 at 22:31
add a comment |
1 Answer
1
active
oldest
votes
Here is how I would approach that. Use the generic class based views to eliminate some of your boilerplate code around form processing (CreateView, UpdateView, or FormView): https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-editing/#createview
Then just add to your context variable in get_context_data to have all of the Post data you want to display. You can then iterate through them in your template to display them with the posts context variable.
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = Profile
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = simplePost.objects.all() # filter this?
return context
Template section for displaying posts:
{% for post in posts %}
{{ post.body }}
{{ post.author }}
...
{% endfor %}
Again i see only form to create post but i don't see posts i will add more code up if this can help^
– Muefi
Nov 13 '18 at 22:46
This single view should give you a form and a list of posts, you just have to use the context variable that contains all your posts in your template. If you want everything on the same page, only one view is needed.
– Victor Bruno
Nov 13 '18 at 22:54
I have template like this but i still dont have posts i see them when i use old method like in IndexView i mean def get_queryset(self): return simplePost.objects.all()
– Muefi
Nov 13 '18 at 23:00
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290368%2fdjango-view-with-form-and-objects%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
Here is how I would approach that. Use the generic class based views to eliminate some of your boilerplate code around form processing (CreateView, UpdateView, or FormView): https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-editing/#createview
Then just add to your context variable in get_context_data to have all of the Post data you want to display. You can then iterate through them in your template to display them with the posts context variable.
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = Profile
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = simplePost.objects.all() # filter this?
return context
Template section for displaying posts:
{% for post in posts %}
{{ post.body }}
{{ post.author }}
...
{% endfor %}
Again i see only form to create post but i don't see posts i will add more code up if this can help^
– Muefi
Nov 13 '18 at 22:46
This single view should give you a form and a list of posts, you just have to use the context variable that contains all your posts in your template. If you want everything on the same page, only one view is needed.
– Victor Bruno
Nov 13 '18 at 22:54
I have template like this but i still dont have posts i see them when i use old method like in IndexView i mean def get_queryset(self): return simplePost.objects.all()
– Muefi
Nov 13 '18 at 23:00
add a comment |
Here is how I would approach that. Use the generic class based views to eliminate some of your boilerplate code around form processing (CreateView, UpdateView, or FormView): https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-editing/#createview
Then just add to your context variable in get_context_data to have all of the Post data you want to display. You can then iterate through them in your template to display them with the posts context variable.
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = Profile
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = simplePost.objects.all() # filter this?
return context
Template section for displaying posts:
{% for post in posts %}
{{ post.body }}
{{ post.author }}
...
{% endfor %}
Again i see only form to create post but i don't see posts i will add more code up if this can help^
– Muefi
Nov 13 '18 at 22:46
This single view should give you a form and a list of posts, you just have to use the context variable that contains all your posts in your template. If you want everything on the same page, only one view is needed.
– Victor Bruno
Nov 13 '18 at 22:54
I have template like this but i still dont have posts i see them when i use old method like in IndexView i mean def get_queryset(self): return simplePost.objects.all()
– Muefi
Nov 13 '18 at 23:00
add a comment |
Here is how I would approach that. Use the generic class based views to eliminate some of your boilerplate code around form processing (CreateView, UpdateView, or FormView): https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-editing/#createview
Then just add to your context variable in get_context_data to have all of the Post data you want to display. You can then iterate through them in your template to display them with the posts context variable.
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = Profile
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = simplePost.objects.all() # filter this?
return context
Template section for displaying posts:
{% for post in posts %}
{{ post.body }}
{{ post.author }}
...
{% endfor %}
Here is how I would approach that. Use the generic class based views to eliminate some of your boilerplate code around form processing (CreateView, UpdateView, or FormView): https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-editing/#createview
Then just add to your context variable in get_context_data to have all of the Post data you want to display. You can then iterate through them in your template to display them with the posts context variable.
class ProfileView(CreateView):
template_name = 'myapp/profile.html'
model_name = Profile
form_class = HomeForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = simplePost.objects.all() # filter this?
return context
Template section for displaying posts:
{% for post in posts %}
{{ post.body }}
{{ post.author }}
...
{% endfor %}
edited Nov 13 '18 at 22:55
answered Nov 13 '18 at 22:33
Victor BrunoVictor Bruno
886610
886610
Again i see only form to create post but i don't see posts i will add more code up if this can help^
– Muefi
Nov 13 '18 at 22:46
This single view should give you a form and a list of posts, you just have to use the context variable that contains all your posts in your template. If you want everything on the same page, only one view is needed.
– Victor Bruno
Nov 13 '18 at 22:54
I have template like this but i still dont have posts i see them when i use old method like in IndexView i mean def get_queryset(self): return simplePost.objects.all()
– Muefi
Nov 13 '18 at 23:00
add a comment |
Again i see only form to create post but i don't see posts i will add more code up if this can help^
– Muefi
Nov 13 '18 at 22:46
This single view should give you a form and a list of posts, you just have to use the context variable that contains all your posts in your template. If you want everything on the same page, only one view is needed.
– Victor Bruno
Nov 13 '18 at 22:54
I have template like this but i still dont have posts i see them when i use old method like in IndexView i mean def get_queryset(self): return simplePost.objects.all()
– Muefi
Nov 13 '18 at 23:00
Again i see only form to create post but i don't see posts i will add more code up if this can help^
– Muefi
Nov 13 '18 at 22:46
Again i see only form to create post but i don't see posts i will add more code up if this can help^
– Muefi
Nov 13 '18 at 22:46
This single view should give you a form and a list of posts, you just have to use the context variable that contains all your posts in your template. If you want everything on the same page, only one view is needed.
– Victor Bruno
Nov 13 '18 at 22:54
This single view should give you a form and a list of posts, you just have to use the context variable that contains all your posts in your template. If you want everything on the same page, only one view is needed.
– Victor Bruno
Nov 13 '18 at 22:54
I have template like this but i still dont have posts i see them when i use old method like in IndexView i mean def get_queryset(self): return simplePost.objects.all()
– Muefi
Nov 13 '18 at 23:00
I have template like this but i still dont have posts i see them when i use old method like in IndexView i mean def get_queryset(self): return simplePost.objects.all()
– Muefi
Nov 13 '18 at 23:00
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290368%2fdjango-view-with-form-and-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
new_item = get_object_or_404(simplePost)
likely fails if there are nosimplePost
objects, or more than one.– Willem Van Onsem
Nov 13 '18 at 22:20
With new_item = get_object_or_404(simplePost) is my bad because i try repair this and i paste wrong code sorry
– Muefi
Nov 13 '18 at 22:25
I think it is probably better to use a
FormView
here, since this will already reduce the view a lot, hence it is easier to spot mistakes in the remaining parts: docs.djangoproject.com/en/2.1/ref/class-based-views/…– Willem Van Onsem
Nov 13 '18 at 22:26
do you want to show saved simplePost object after it will be created?
– Alexander Tyapkov
Nov 13 '18 at 22:28
Hmm yes i used FormView i see my form in template but i dont see objects i mean posts from database only when i delete all functions associated with form i see my posts
– Muefi
Nov 13 '18 at 22:31