When I was working through the django docs, it occurred to me that I’ve been using an old version of django (1.11) rather than the newest 2.0. So I decided to rebuild the app using django 2 before I got too far into it. Most things went relatively smoothly, plus it gave me practice in retyping things. After I rebuild the user registration part, it was time to start allowing users to post blogs
My general process is
The model was straight forward. Right now I just have Title, Content, Author, Created Date, and Published Date. I’ll probably add tags and some other stuff later, but that’s good enough for now.
For View, I decided to use the generic . CreateView to handle this, but that led to some problems with the Form which was the hardest part so far. The template and URLs stuff was pretty straight forward so my main problem today was trying to link my author to the foreign key user, but every time I went to save the post, I got this author_id Not Null error. My problem was that I was excluding the author from the new post form, so when I went to save there was no author selected. Once I added it back, I could select the author of the post and submit fine, but that’s not really an idea situation. Users should log in but not choose who is the author of the post.
What I wanted to do was have the current user logged in automatically saved as the author. So after a lot of googling, I found out that I can override the default form_valid method.