samedi 24 mars 2018

Issue with getting author when posting (Django)

Sorry to bother you all but I'm having an issue I have absolutely no understanding of. This is going to be fairly long so if you don't have a large amount of time on your hands please don't hesitate to ignore this question.

My issue is with a tutorial for Django. I've already completed it once, so I've been trying to put a spin on it before learning more and completely rewriting it myself. I've removed a non-essential but good for organization app from the project called pages, as well as styling the actual templates. Nothing complicated, and nothing to do with the underlying way any of their code works. Time for where the actual question comes in, however first, here is the link to:

The tutorial I'm following: http://intermediatedjango.com/

I was planning to link to more, but nothing but that is really relevant. In following this tutorial I've gotten to a section where you replace a part of the code you've written in the views.py file of the posts app. That section is replacing:

class PostCreateView(CreateView):
    model = models.Post
    template_name = 'post_new.html'
    fields = ['message', 'author']

With the following: https://pastebin.com/UE5cvaQ4

After doing this and going to the page to create a new post and doing so I received the error: "Django NOT NULL constraint failed". After messing around with the code a small amount and looking for an answer here one proposed solution for a similar problem was to add "null = True, blank= True" and run makemigrations and migrate. This sort of worked as in it resolved the error. However, the author section of posts just became "None". Now my first guess was that for some reason I was not being recognized as logged in and it couldn't read my username. So I went into the template and put the following in: . To my surprise, the result was as follows:

What occurred when I added the code to the template for creating new posts

I have no idea what's going on now. It knows my username, and I'm logged in. How is this possible? How can I resolve this?




Aucun commentaire:

Enregistrer un commentaire