django issue cant insert comment from url
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have been following django girls tutorial and I run into comment issue. I can add comment to the post from Admin interface but I cant add it from the form.
Here is my model:
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#author = models.CharField(max_length=200)
ctext = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.ctext
Here is my form:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('ctext',)
And a view:
def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST, instance=post)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.post = post
comment.save()
return redirect('post_detail', pk=post.pk)
else:
form = CommentForm()
return render(request, 'blog/add_comment_to_post.html', {'form': form})
Any advice will be helpful. Thank you.
django python-3.x
|
show 4 more comments
I have been following django girls tutorial and I run into comment issue. I can add comment to the post from Admin interface but I cant add it from the form.
Here is my model:
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#author = models.CharField(max_length=200)
ctext = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.ctext
Here is my form:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('ctext',)
And a view:
def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST, instance=post)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.post = post
comment.save()
return redirect('post_detail', pk=post.pk)
else:
form = CommentForm()
return render(request, 'blog/add_comment_to_post.html', {'form': form})
Any advice will be helpful. Thank you.
django python-3.x
Where is it failing? Are you getting a 404 or form is invalid? What's the form data that you are passing in the request?
– Sachin
Nov 16 '18 at 21:57
no failing at all... and not log in postgres so it even does not go to db. I have no idea how to debug it. In the form I am passing only the comment text. I assume that userid and postid is somehow should be there.
– nanda
Nov 16 '18 at 22:07
Add some print statements to figure out.
– Sachin
Nov 16 '18 at 22:08
how I do the print statements in django's fw?
– nanda
Nov 16 '18 at 22:10
For example, addprint(form)
after you initialize the form.
– Sachin
Nov 16 '18 at 22:10
|
show 4 more comments
I have been following django girls tutorial and I run into comment issue. I can add comment to the post from Admin interface but I cant add it from the form.
Here is my model:
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#author = models.CharField(max_length=200)
ctext = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.ctext
Here is my form:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('ctext',)
And a view:
def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST, instance=post)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.post = post
comment.save()
return redirect('post_detail', pk=post.pk)
else:
form = CommentForm()
return render(request, 'blog/add_comment_to_post.html', {'form': form})
Any advice will be helpful. Thank you.
django python-3.x
I have been following django girls tutorial and I run into comment issue. I can add comment to the post from Admin interface but I cant add it from the form.
Here is my model:
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#author = models.CharField(max_length=200)
ctext = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.ctext
Here is my form:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('ctext',)
And a view:
def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST, instance=post)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.post = post
comment.save()
return redirect('post_detail', pk=post.pk)
else:
form = CommentForm()
return render(request, 'blog/add_comment_to_post.html', {'form': form})
Any advice will be helpful. Thank you.
django python-3.x
django python-3.x
edited Nov 16 '18 at 23:01
nanda
asked Nov 16 '18 at 21:43
nandananda
217
217
Where is it failing? Are you getting a 404 or form is invalid? What's the form data that you are passing in the request?
– Sachin
Nov 16 '18 at 21:57
no failing at all... and not log in postgres so it even does not go to db. I have no idea how to debug it. In the form I am passing only the comment text. I assume that userid and postid is somehow should be there.
– nanda
Nov 16 '18 at 22:07
Add some print statements to figure out.
– Sachin
Nov 16 '18 at 22:08
how I do the print statements in django's fw?
– nanda
Nov 16 '18 at 22:10
For example, addprint(form)
after you initialize the form.
– Sachin
Nov 16 '18 at 22:10
|
show 4 more comments
Where is it failing? Are you getting a 404 or form is invalid? What's the form data that you are passing in the request?
– Sachin
Nov 16 '18 at 21:57
no failing at all... and not log in postgres so it even does not go to db. I have no idea how to debug it. In the form I am passing only the comment text. I assume that userid and postid is somehow should be there.
– nanda
Nov 16 '18 at 22:07
Add some print statements to figure out.
– Sachin
Nov 16 '18 at 22:08
how I do the print statements in django's fw?
– nanda
Nov 16 '18 at 22:10
For example, addprint(form)
after you initialize the form.
– Sachin
Nov 16 '18 at 22:10
Where is it failing? Are you getting a 404 or form is invalid? What's the form data that you are passing in the request?
– Sachin
Nov 16 '18 at 21:57
Where is it failing? Are you getting a 404 or form is invalid? What's the form data that you are passing in the request?
– Sachin
Nov 16 '18 at 21:57
no failing at all... and not log in postgres so it even does not go to db. I have no idea how to debug it. In the form I am passing only the comment text. I assume that userid and postid is somehow should be there.
– nanda
Nov 16 '18 at 22:07
no failing at all... and not log in postgres so it even does not go to db. I have no idea how to debug it. In the form I am passing only the comment text. I assume that userid and postid is somehow should be there.
– nanda
Nov 16 '18 at 22:07
Add some print statements to figure out.
– Sachin
Nov 16 '18 at 22:08
Add some print statements to figure out.
– Sachin
Nov 16 '18 at 22:08
how I do the print statements in django's fw?
– nanda
Nov 16 '18 at 22:10
how I do the print statements in django's fw?
– nanda
Nov 16 '18 at 22:10
For example, add
print(form)
after you initialize the form.– Sachin
Nov 16 '18 at 22:10
For example, add
print(form)
after you initialize the form.– Sachin
Nov 16 '18 at 22:10
|
show 4 more comments
1 Answer
1
active
oldest
votes
Sachin Kukreja in the comments suggested to remove instance=post from Comment form and this solve the issue. Thank you!
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%2f53345825%2fdjango-issue-cant-insert-comment-from-url%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
Sachin Kukreja in the comments suggested to remove instance=post from Comment form and this solve the issue. Thank you!
add a comment |
Sachin Kukreja in the comments suggested to remove instance=post from Comment form and this solve the issue. Thank you!
add a comment |
Sachin Kukreja in the comments suggested to remove instance=post from Comment form and this solve the issue. Thank you!
Sachin Kukreja in the comments suggested to remove instance=post from Comment form and this solve the issue. Thank you!
answered Nov 16 '18 at 23:13
nandananda
217
217
add a comment |
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%2f53345825%2fdjango-issue-cant-insert-comment-from-url%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
Where is it failing? Are you getting a 404 or form is invalid? What's the form data that you are passing in the request?
– Sachin
Nov 16 '18 at 21:57
no failing at all... and not log in postgres so it even does not go to db. I have no idea how to debug it. In the form I am passing only the comment text. I assume that userid and postid is somehow should be there.
– nanda
Nov 16 '18 at 22:07
Add some print statements to figure out.
– Sachin
Nov 16 '18 at 22:08
how I do the print statements in django's fw?
– nanda
Nov 16 '18 at 22:10
For example, add
print(form)
after you initialize the form.– Sachin
Nov 16 '18 at 22:10