The thing you get from your request here is likely a string. You’ll need to use that string to do an Author.objects.get to get an actual object to pass in.
When you get an error like that, usually the error is right. The User does not exist. Check what is going into the query and whether you can reproduce that in the shell.
I think that when you are using the <label> tag here, you actually mean to use something else, maybe an <input> tag? (and in that case maybe with a default attribute set to {{ user.username }} ). You can read about what the <label> tag does here: https://www.w3schools.com/tags/tag_label.asp This is what is causing the error 'NoneType' object has no attribute 'id', I believe.
Once you’ve fixed this, you should also change this part:
if request.method == "POST":
request_post_author = request.POST.get("post_author")
new_post_author = User.objects.get(request_post_author.id)
I think you instead need to use
if request.method == "POST":
request_post_author = request.POST.get("post_author")
new_post_author = User.objects.get(username=request_post_author)
Also, I think that the overall approach you’ve used might not be the most appropriate for what you want to achieve.
If you want the user to be able to specify not only him/herself, but any of the registered users, as the author, you probably want to use a Form class whose instances can fetch the usernames of currently existing users from the database, and create a single-choice list of usernames to select from. (or you could add code in your view to query the database, and add tags in your template, but I think at that point using a Form might be better) Otherwise, the user might try to input a username that doesn’t exist in the database, producing an error.
If you always want the current user to be registered as the author of the post, I’m guessing you would be better off not including anything about that in the form, and instead using request.user in your view to get information (e. g. the pk/id) about the current user. If I’ve understood how that can be used correctly that is, I haven’t used it much myself but here’s a relevant link to the django docs https://docs.djangoproject.com/en/3.0/topics/auth/default/#s-authentication-in-web-requests
I think you’ll find things will work a whole lot easier if you create your page using a Django form rather than rendering the fields manually and trying to process the POST elements yourself. That’s one of the real benefits of working with a framework like Django - it does a lot of the “housekeeping” that you would otherwise need to do yourself.
Create a template that renders the form as part of the template
Create a view that renders the template on a GET, and saves the data from the form on a POST
(Or, better yet, start getting familiar with the generic Class-Based Views and implement this page as a DetailView. Or, take a look at the django vanilla views package.)
Now the data is indeed sent to the request.POST attribute as a dictionnary.
And the forms.py file :
from django.forms import ModelForm,Textarea
from .models import PostModel
class PostForm(ModelForm):
class Meta:
model = PostModel
fields = "__all__"
widgets = {
"post_message": Textarea(attrs={
"rows":10,
"cols":40,
"maxlength":140,
}),
}
My last issue here is that :
{{ PostForm.post_author }}
Will give me an HTML list of every User objects present in my db. What I’d like is to say Hey I want you to automatically choose the current logged in user to be the post_author
How can I do that ?
EDIT :
As @datalowe said above (Thank you by the way ), one can get the logged in user by using request.user like this :