Home>
I'm studying django. I'm thinking of adding a comment function to write an impression on the posted Post model
in models.py
class Comment (models.Model):
user = models.ForeignKey (settings.AUTH_USER_MODEL, on_delete = models.CASCADE, related_name ='comment_user')
post = models.ForeignKey (Post, on_delete = models.CASCADE)
date_created = models.DateTimeField (auto_now_add = True)
text = models.TextField ()
def __str __ (self):
return self.text
And add to forms.py
class CommentForm (forms.ModelForm):
class Meta:
model = Comment
fields = ('text',
)
in views.py
def comment (request, pk):
if request.method == "POST":
form = CommentForm (request.POST)
if form.is_valid ():
comment = form.save (commit = False)
comment.user = request.user
comment.date_created = timezone.now ()
posts = Post.objects.filter (pk = pk)
comment.post = posts.first ()
comment.save ()
return redirect ('post_detail', pk = posts.pk)
else: else:
form = CommentForm ()
return render (request,'mybook/comment.html', {'form': form})
In the view, I intended to get the pk of the post page, save the comment for that post, and then move to the detail page (post_detail) of that post.
return redirect ('post_detail', pk = posts.pk)
By the way
'QuerySet' object has no attribute'pk'
I got the error.
I think it means that there is no pk in the original post (Query set) obtained by posts = Post.objects.filter (pk = pk), but I am in trouble because I do not know the cause. Thank you.
-
Answer # 1
Related articles
- laravel - i get the error call to a member function load () on null
- apache - selenium called 500 error from django
- function - although it is mql4 language, i get an error when compiling with this code
- php fatal error: call to a member function fetchrow () does not show site
- c ++ - error in loaddivgraph function
- [django] i want to insert a comment form on the blog detail page
- php header function error
- python - i don't know how to use requestuser in a django function with a generic view
- error when implementing search function using ransack in ruby on rails
- javascript - map is not a function error
- [django] an error is displayed when i try to save model data
- python - i get an argument must be int or float error in django
- ruby - if you try to delete a comment post, you will get an error `destroy' for nil:nilclass please solve it
- php - error when implementing line login function by socialite
- ruby - data is not stored when implementing the comment function
- ruby on rails - comment function deletion
- javascript - i want to implement the comment function by asynchronous communication
- python - pandas read_csv function error reading data_csv file (macos)
- python - file read error using read function (mac)
Trends
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
What if you do the following?